search and rename files - bash

I've found a bad habit file naming among the users of the QNAP Linux NAS system I'm administering.
We have a Mac OS network, and some folders and files are named using the "/" character that I know it's causing problems to Linux file system.
As a matter of fact, from the Mac OS side, the files containing "/" simply disappear, since QTS replaces automatically all instances with "/" with ":".
I'd like to search and rename filenames from ":" to "_".
Discussing on the web I've found that I can SSH connect to the Linux NAS from my Mac with Terminal and perform a script like this:
for f in $(find /share/Public/demofind -name "*:*"); do mv $f ${f/:/_}; done
assuming that the files are in /demofind folder.
I launched the script, but got this error:
[/share/Public] # for f in $(find /share/Public/demofind/ -name "*:*"); do mv $f ${f/:/_}; done
mv: unable to rename `/share/Public/demofind/Redazionali': No such file or directory
mv: unable to rename `01:03:19.pdf': No such file or directory
mv: unable to rename `/share/Public/demofind/Redazionali': No such file or directory
mv: unable to rename `06:09:19.pdf': No such file or directory
[/share/Public] # for f in $(find /share/Public/demofind/ -name "*:*"); do mv $f ${f/:/_}; done
mv: unable to rename `/share/Public/demofind/Redazionali': No such file or directory
mv: unable to rename `01:03:19.pdf': No such file or directory
mv: unable to rename `/share/Public/demofind/Redazionali': No such file or directory
mv: unable to rename `06:09:19.pdf': No such file or directory
By the way, the files to rename have a syntax like this: "Redazionali 06:09:19.pdf"
The NAS seems to be running BusyBox v1.01 (2021.12.12-04:24+0000) so I would need a solution which is compatible with this platform.

The immediate problem is that you have broken quoting but your code has other problems too.
The robust solution would be
find /share/Public/demofind -name "*:*" -depth -exec bash -c '
for f; do mv "$f" "${f//:/_}"; done' _ {} +
Putting the for loop inside find -exec works around pesky problems around file names with unusual characters in them, not just spaces (though your for loop would also inherently break on just single spaces, too; you would have to separately tell the shell to not perform whitespace tokenization if you used the output from find in a command substitution, but again, the proper fix is to simply not do that).
For details, please review https://mywiki.wooledge.org/BashFAQ/020
The -depth option to find says to traverse the directory tree depth-first; this is important when renaming, so that you don't end up renaming directories before you rename the files within them, which then leads to errors when the file you want to rename no longer exists where it was originally found.
The parameter expansion ${f//:/_} is a Bash feature; if you don't have Bash, you have to use an external tool (or a really hairy function around the more pedestrian parameter expansion facilities of POSIX sh; but let's not go there).
find /share/Public/demofind -name "*:*" -depth -exec sh -c '
for f; do mv "$f" "$(echo "$f" | tr : _)"; done' _ {} +
If your platform doesn't even support find -exec you can manually loop over the output from find but again, see the FAQ link above for a number of caveats.
find /share/Public/demofind -name "*:*" -depth -print |
while IFS='' read -r f; do
mv "$f" "$(echo "$f" | tr : _)"
done

Related

usage error when moving files of one extension to a directory

I have files with extension .mp3 that are in different folders within a directory, and I need to move them all to one directory to work with. I have looked at multiple tutorials and questions on SO, and no matter what I try, I either get
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
or "No such file or directory".
There are way too many to into each individual folder, but for now I cd into one of the folders. With this:
mv *.mp3 /Users/myname/Volumes/LaCie/model/folder/media
I get the "usage" error above. I looked at Moving files to a directory and tried:
find . | grep ".mp3" | xargs mv /Users/myname/Volumes/LaCie/model/folder/media
and get same error.
What am I doing wrong? What is the correct syntax? Also, will I be able to extract the mp3 files and move them if I'm in a directory that contains the directory with the files, but not in that directory itself? I appreciate insights into this. Thanks.
EDIT: A major part of this issue was that the path when using /Volumes starts with /Volumes. I was doing /Users/myname/Volumes and that was one reason I had so much trouble.
The problem with the way you're using xargs is that by default, xargs will append the arguments to the end of the command string you've provided it. So you'll end up running a bunch of mv commands that look like this:
mv /Users/myname/Volumes/LaCie/model/folder/media foo.mp3
You can fix that by telling xargs where to place the arguments within the command:
<other commands> | xargs -I{} mv {} /Users/myname/Volumes/LaCie/model/folder/media
The -I option lets you provide any arbitrary string as a placeholder for where the args should go. I used {} just because that seems to be the conventional token that you see used in similar contexts (such as with the -exec option of find, as shown below).
But there's an easier way to do it, using the find command's -exec option:
find . -name '*.mp3' -exec mv {} /Users/myname/Volumes/LaCie/model/folder/media \;
Also note the -name '*.mp3' part, which lets you get rid of the | grep ".mp3" part.
Lastly, just to be safe, I'd personally put a / at the end of your destination path. If the media directory doesn't exist in /Users/myname/Volumes/LaCie/model/folder, or if a non-directory item (such as a regular file or a symlink) named media exists in that location, then the find command above will happily just move all your mp3 files, one at a time, to that folder, creating a file named media there each time. And you will have lost all of your mp3 files except for the last one, which will now be a file named media.
However, with a trailing /, if media is not a directory, the mv commands will fail with an error saying so. So the revised command would be:
find . -name '*.mp3' -exec mv {} /Users/myname/Volumes/LaCie/model/folder/media/ \;
Update: Per Gordon Davisson's comment below, you should also consider adding -i or -n to the mv command, to avoid accidentally overwriting files with duplicate names. For example, if you have a/foo.mp3 and b/foo.mp3, the above command will overwrite one with the other. The -i option will cause mv to prompt you to confirm each file move, whereas the -n option (a.k.a. --no-clobber) will prevent mv from overwriting a file if a file with the same name already exists.
There are different implementations and versions of mv. You can check the allowed syntax of your version using man mv.
If you have GNU mv you could use mv -t target/dir *.mp3.
Most implementations should support mv *.mp3 target/dir.
If your mv only supports the absolute minimum of mv source target with exactly one source and one target file you can use the following command which should always work if target/dir/ exists.
for i in *.mp3; do mv "$i" "target/dir/$i"; done

Shell command (on Mac OSX El Capitan) to recursively rename all my DOCX files

I am trying to build a shell command (on Mac OSX El Capitan) to recursively rename all my DOCX files to have extension ZZZZ and then to immediately rename them back again to the DOCX extension. This is a workaround to hopefully fix a problem as follows:
I am doing this to try to get around a Mac Spotlight bug which doesn't search for content inside Mac Word 2011 files correctly. It gives intermittent results and seem to miss a lot of hits (this issue seem to be well-known for a few years on Apple Mac Forums). Renaming a file seems to kick-start Spotlight into action.
Mac Shell doesn't have the BASH Rename command so I am trying to iteratively use the "MV" command. I've had partial success with the following code but don't know how to tie it together...
cd ~/Documents/TESTING/
# FINDS MY DOCX'S RECURSIVELY IN TOP-LEVEL FOLDER AND IT'S SUBFOLDERS. NOT SURE OF SYNTAX TO USE FOR "MV" COMMAND TO RENAME DOCX FILES
# find . -wholename '*.docx' -type f -exec mv UNSURE1HERE UNSURE2HERE \;
# WORKS BUT ONLY IN TOP-LEVEL FOLDER - I NEED IT TO WORK RECURSIVELY ON DOCX'S IN TOP-LEVEL FOLDER AND IT'S SUBFOLDERS:
# for files in *.docx; do mv "$files" "${files%.docx}.zzzz"; done
You can do this using process substitution:
#!/bin/bash
cd ~/Documents/TESTING/
while IFS= read -r -d '' file; do
echo mv -- "$file" "${file%.*}.zzzzz"
done < <(find . -iname '*.docx' -type f -print0)
If you're satisfied with the output then remove echo before mv
From .zzzz to .docx:
while IFS= read -r -d '' file; do
echo mv -- "$file" "${file%.*}.docx"
done < <(find . -iname '*.zzzz' -type f -print0)

Copy all files with a certain extension from all subdirectories

Under unix, I want to copy all files with a certain extension (all excel files) from all subdirectories to another directory. I have the following command:
cp --parents `find -name \*.xls*` /target_directory/
The problems with this command are:
It copies the directory structure as well, and I only want the files (so all files should end up in /target_directory/)
It does not copy files with spaces in the filenames (which are quite a few)
Any solutions for these problems?
--parents is copying the directory structure, so you should get rid of that.
The way you've written this, the find executes, and the output is put onto the command line such that cp can't distinguish between the spaces separating the filenames, and the spaces within the filename. It's better to do something like
$ find . -name \*.xls -exec cp {} newDir \;
in which cp is executed for each filename that find finds, and passed the filename correctly. Here's more info on this technique.
Instead of all the above, you could use zsh and simply type
$ cp **/*.xls target_directory
zsh can expand wildcards to include subdirectories and makes this sort of thing very easy.
From all of the above, I came up with this version.
This version also works for me in the mac recovery terminal.
find ./ -name '*.xsl' -exec cp -prv '{}' '/path/to/targetDir/' ';'
It will look in the current directory and recursively in all of the sub directories for files with the xsl extension. It will copy them all to the target directory.
cp flags are:
p - preserve attributes of the file
r - recursive
v - verbose (shows you whats
being copied)
I had a similar problem. I solved it using:
find dir_name '*.mp3' -exec cp -vuni '{}' "../dest_dir" ";"
The '{}' and ";" executes the copy on each file.
I also had to do this myself. I did it via the --parents argument for cp:
find SOURCEPATH -name filename*.txt -exec cp --parents {} DESTPATH \;
In 2022 the zsh solution also works in Linux Bash:
cp **/*.extension /dest/dir
works as expected.
find [SOURCEPATH] -type f -name '[PATTERN]' |
while read P; do cp --parents "$P" [DEST]; done
you may remove the --parents but there is a risk of collision if multiple files bear the same name.
On macOS Ventura 13.1, on zsh, I saw the following error when there were too many files to copy, saw the following error:
zsh: argument list too long: cp
Had to use find command along with cp to get the files copied to my destination:
find ./module/*/src -name \*.java -print | while read filelocation; do cp $filelocation mydestinationlocation; done

Bash scripting, loop through files in folder fails

I'm looping through certain files (all files starting with MOVIE) in a folder with this bash script code:
for i in MY-FOLDER/MOVIE*
do
which works fine when there are files in the folder. But when there aren't any, it somehow goes on with one file which it thinks is named MY-FOLDER/MOVIE*.
How can I avoid it to enter the things after
do
if there aren't any files in the folder?
With the nullglob option.
$ shopt -s nullglob
$ for i in zzz* ; do echo "$i" ; done
$
for i in $(find MY-FOLDER/MOVIE -type f); do
echo $i
done
The find utility is one of the Swiss Army knives of linux. It starts at the directory you give it and finds all files in all subdirectories, according to the options you give it.
-type f will find only regular files (not directories).
As I wrote it, the command will find files in subdirectories as well; you can prevent that by adding -maxdepth 1
Edit, 8 years later (thanks for the comment, #tadman!)
You can avoid the loop altogether with
find . -type f -exec echo "{}" \;
This tells find to echo the name of each file by substituting its name for {}. The escaped semicolon is necessary to terminate the command that's passed to -exec.
for file in MY-FOLDER/MOVIE*
do
# Skip if not a file
test -f "$file" || continue
# Now you know it's a file.
...
done

How to consolidate selected files from multiple sub-directories into one directory

I know this is probably elementary to unix people, but I haven't found a straightforward answer online.
I have a directory with sub-directories. Some of these sub-dirs have .mov files in them. I want to consolidate all the movs to a single directory. I don't need to worry about file naming conflicts because the files are from a digital camera and it names the files incrementally, but divides them into daily folders.
What is the Unix-fu for grabbing all these files and copying (or even better, moving them) to a directory in my home folder?
Thanks.
How about this?
find "$SOURCE_DIRECTORY" -type f -name '*.mov' -exec mv '{}' "$TARGET_DIRECTORY" ';'
If the source and target directories do not overlap this should work fine.
EDIT:
BTW, if you have mixed-case extensions (x.mov, y.Mov, Z.MOV) as is the case with many cameras, this would be better. It uses -iname which is case-insensitive when matching:
find "$SOURCE_DIRECTORY" -type f -iname '*.mov' -exec mv '{}' "$TARGET_DIRECTORY" ';'
Make sure to replace the $SOURCE_DIRECTORY and $TARGET_DIRECTORY variables with the actual directories and that they do not overlap (i.e. the target being somewhere under the source)
EDIT 2:
PS: I just noticed that khachik caught this one with his edit
mv `find . -name "*.mov" | xargs` OUTPUTDIR/
Update after thkala's comment:
find . -iname "*.mov" | while read line; do mv "$line" OUTPUTDIR/; done
If you need to cope with weird filenames (spaces, special characters), try this:
$ cd <source parent directory>
$ find -name '*.mov' -print0 | xargs -0 echo mv -v -t <target directory>
Remove the "echo" above to actually do the move, rather than print what would happen.
"mv -v" gives verbose output, "mv -t ..." specifies the target directory (possibly GNU-specific).
"-print0" and "-0" are extensions to cope with weird filenames. On non-GNU systems you might need to remove those options, which will result in newline-separated data. This will still work on filenames with spaces, but not filenames with newlines (yes, it's possible).

Resources