Find command with prune option is not working in particular directory in KSH - find

I want to list down .success files in dly folder and not within dly folder recursively as below.
find /CD/dly/ * prune -name "*.success"
Above command is listing file list properly. However when I switch to /AB/dly/ and run the same command
cd CD AB
find /AB/dly/ * prune -name "*.success"
It is showing below error.
find: bad option -ltr
Is it problem with AB directory? or something else?

Related

Git Bash find exec recursively on folders and files containing spaces

Question: In Git Bash on windows, how would you run the following in a way that it will also search folders with spaces in the name, and execute on files with spaces in the name?
$ find ./ -type f -name '*.png' -exec sh -c 'cwebp -q 75 $1 -o "${1%.png}.webp"' _ {} \;
Context I'm running Git Bash on windows, trying to execute a command on all found .png files to convert them to .webp format. It works for all files without spaces in the path, but it's failing to find files with spaces in the filename or files within folders that have spaces in the folder name.A few considerations:
I have many, many levels of folders to iterate through, and I can't run this command separately for each. I really need the recursion to work.I cannot change the folder names; it will break other dependencies (nor did I create the folder or filenames originally, so cut me some slack!)I arrived here by following the suggestions from this article: https://www.smashingmagazine.com/2018/07/converting-images-to-webp/the program, to my knowledge, doesn't ship with any built-in recursive command... golly that'd be handy
Any help you can provide will be appreciated. Thanks!

Using "rm" to remove files remotely from another directory?

I'm unable to use the rm command to remove files remotely from another directory. I'm a beginner so I apologise for my inability to elaborate properly.
Here's what I'm trying to do:
I'm trying to delete all .srt files from a sub directory. It works when I cd into the specific directory like so:
Command 1:
cd /users/jakubdonovan/library/cloudstorage/iCloud\ drive/the-modern-python3-bootcamp/target_folder
Command 2:
rm *.srt
However, let's say I want to quickly delete a specific file type from a folder without first using the "cd" command, like so:
rm *.srt /users/jakubdonovan/library/cloudstorage/iCloud\ drive/the-modern-python3-bootcamp/target_folder
It returns with "No matches for wildcard '*.srt'. See help expand."
Which is strange because I can use the touch, cp and and all the other commands remotely without a problem.
Is there a way to make the command "rm *.filetype" remove all the files with that specific filetype from a folder and all its subfolders in one swoop?
If you would like to rm in a sub-directory you just have to specify that sub-directory in the command.
rm /path/to/folder/*.filetype
or if you know that the folder is inside your current directory you can try...
rm ./folder/*.filetype

find -type d and ls return different results on same directory

I'm trying to think of a reason find would return partial results on Ubuntu 16.04. I have a google drive mount (using plexdrive) located at
/home/user/media
When using
find $HOME/media -type d
16 results are returned. When using
ls -d $HOME/media/*/
186 results are returned. The folders are all browsable via sftp and samba shares, all permissions on relevant directories are 775. The content in the media directory is formatted as
a/a.ext
b/b.ext
c/c.ext
Has anyone seen this type of behavior before or have any clues on how to get find to return the full results?
This happens when find is looking at a directory with symbolic links. Use the -L flag to see the proper folder structure. find -L $HOME/media -type d

Recursively replace colons with underscores in Linux

First of all, this is my first post here and I must specify that I'm a total Linux newb.
We have recently bought a QNAP NAS box for the office, on this box we have a large amount of data which was copied off an old Mac XServe machine. A lot of files and folders originally had forward slashes in the name (HFS+ should never have allowed this in the first place), which when copied to the NAS were all replaced with a colon.
I now want to rename all colons to underscores, and have found the following commands in another thread here: pitfalls in renaming files in bash
However, the flavour of Linux that is on this box does not understand the rename command, so I'm having to use mv instead. I have tried using the code below, but this will only work for the files in the current folder, is there a way I can change this to include all subfolders?
for f in *.*; do mv -- "$f" "${f//:/_}"; done
I have found that I can find al the files and folders in question using the find command as follows
Files:
find . -type f -name "*:*"
Folders:
find . -type d -name "*:*"
I have been able to export a list of the results above by using
find . -type f -name "*:*" > files.txt
I tried using the command below but I'm getting an error message from find saying it doesn't understand the exec switch, so is there a way to pipe this all into one command, or could I somehow use the files I exported previously?
find . -depth -name "*:*" -exec bash -c 'dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//:/_}"' _ {} \;
Thank you!
Vincent
So your for loop code works, but only in the current dir. Also, you are able to use find to build a file with all the files with : in the filename.
So, as you've already done all this, I would just loop over each line of your file, and perform the same mv command.
Something like this:
for f in `cat files.txt`; do mv $f "${f//:/_}"; done
EDIT:
As pointed out by tripleee, using a while loop is a better solution
EG
while read -r f; do mv "$f" "${f//:/_}"; done <files.txt
Hope this helps.
Will

Compressing only files using 7z without preserving the path

I am using 7z command line executable to zip files, but I see that while adding to an archive the path of the files is preserved in the archive.
So if I do
7z a -tzip myzip.zip dir1\dir2\*
the archive myzip.zip will contain the path dir1\dir2. I do not want this, rather I want only the files to be added to the zip file without the paths being preserved.
I searched quite a bit but do not seem to find any way of doing this, maybe I am missing something obvious?
Thanks
Just add a dot before the path, i.e.
7z a -tzip -r myzip.zip .\Relative\Dir\*
Give the full path. That should work. Not the relative path from the current location.
For example, I give the below, where I want the files in the man5 folder to be archived.
$ 7z a -tzip myzip.zip /home/pradeeban/Desktop/man4/man5/*
The zip contained only the files, without the directories.
Then I gave only the relative path. It had the directories, inside the zip.
$ 7z a -tzip myzip.zip Desktop/man4/man5/*
Tried with Linux (Ubuntu 12.04). Not sure whether that differs from Windows.
I discovered a way to do this by using a relative path:
7z a -tzip myzip.zip %CD%\dir1\dir2\*
%CD% is how you get the current path in a Windows batch file, but it also works from the command line. More info about Capturing the current directory from a batch file.
As explained in related question in 7-zip user FAQ, 7z stores paths relative to working directory, so you will need to first cd to desired top-level directory for archive and run 7-zip from here.
cd dir1\dir2\
7z a -tzip myzip.zip *
If you run it from script and don't want to affect it with changed directory, use directory push/pop facilities available in your shell of choice or run cd+7-zip in spawned process to avoid affecting your entire script with changed directory. For example, using Windows' start that would be:
start /D dir1\dir2\ /wait 7z a -tzip myzip.zip *
This worked for me
Consider folder structure like C:\Parent\SubFolders..... And you want to create parent.zip which will contain all files and folders C:\Parent without parent folder [i.e it will start from SubFolders.....]
cd /D "C:\Parent"
"7z.exe" a Parent.zip "*.*" -r
This will create Parent.zip in C:\Parent