Move all files NOT ending with .txt
The simple shell loop variant (in bash
):
shopt -s extglob dotglob nullglob
for pathname in ~username/data/!(*.txt); do
! test -d "$pathname" && mv "$pathname" ~username/data/other_files
done
The shell options set on the first line will make the bash
shell enable extended globbing patterns (!(*.txt)
to match all names not ending with .txt
), it enables glob patterns to match hidden names, and it makes the pattern expand to nothing at all if nothing matches.
The body of the loop will skip anything that is a directory (or symbolic link to a directory) and will move everything else to the given directory.
The equivalent thing with find
and GNU mv
(will move symbolic links to directories if there are any, and will invoke mv
for as many files as possible at a time, but those are the only differences):
find ~username/data -maxdepth 1 ! -type d ! -name '*.txt' \
-exec mv -t ~username/data/other_files {} +
Related:
- Understanding the -exec option of `find`
find /home/username/data -maxdepth 1 -type f ! -name '*.txt' -exec mv {} /home/username/data/other_files/ \;
- maxdepth limits to the top directors
- type ensures that only files are found, not directories
This code should move all files not ending in ".txt" to your target folder, however if you happen to have files with the same name in different paths it will throw an error.
find /home/username/data ! -name "*.txt" -type f -maxdepth 1 -exec mv {} /home/username/data/other_files/ \;