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 $src
is 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 thefind
command instead. What you wrote is extremely hackish.– slm ♦Commented Jul 17, 2015 at 8:06 -
mv src/dir/* dst/dir/
not take along?