2

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.

2
  • What have you tried? What is not working? Do you have some files that mv src/dir/* dst/dir/ not take along?
    – Anthon
    Commented Jul 17, 2015 at 5:57
  • I think this is most stupid non-feature in the whole linux universe. To write a script to move a file.. what? Commented May 15, 2022 at 12:09

4 Answers 4

3

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 \;
0
1

Using find:

find . -maxdepth 1 -type f -print0 | xargs -0 -I {} mv {} /path/to/target/directory/{}

This will also deal with filenames containing newlines.

0
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

0
$ cd $SOURCE_DIR
$ mv `ls -p| grep -v /` $Target_dir

Execution Steps

  1. Move to Source directory using cd command.
  2. ls -p suffixes "/" to all the directories.
  3. grep -v is used exclude the directories and to get the regular file.
  4. Finally moving all the files to the target directory.
2
  • Don't do mv `....` ..... Use the find command instead. What you wrote is extremely hackish.
    – slm
    Commented Jul 17, 2015 at 8:06
  • why is it hackish? Commented May 15, 2022 at 12:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .