How do you move all the files (excluding sub-directories) from one directory to another. I'd prefer if the solution used just basic shell scripting.
4 Answers
The easy way:
for f in /some/path/*; do
if [ -f "$f" ]; then
mv "$f" /some/other/path
fi
done
The slightly more complicated way:
find /some/path -mindepth 1 -maxdepth 1 -type f -exec mv {} /dome/other/path \;
Using find:
find . -maxdepth 1 -type f -print0 | xargs -0 -I {} mv {} /path/to/target/directory/{}
This will also deal with filenames containing newlines.
cd $src
mv `find . -maxdepth 1 -type f -printf '%f\n'` $tgt
should do it, where $srcis your source folder and $tgt is your target folder
$ cd $SOURCE_DIR
$ mv `ls -p| grep -v /` $Target_dir
Execution Steps
- Move to Source directory using cd command.
- ls -p suffixes "/" to all the directories.
- grep -v is used exclude the directories and to get the regular file.
- Finally moving all the files to the target directory.
-
Don't do
mv `....` ..... Use thefindcommand instead. What you wrote is extremely hackish.2015-07-17 08:06:26 +00:00Commented Jul 17, 2015 at 8:06 -
why is it hackish?Boppity Bop– Boppity Bop2022-05-15 12:07:11 +00:00Commented May 15, 2022 at 12:07
mv src/dir/* dst/dir/not take along?