xzhu
xzhu

Reputation: 5755

How do I select all the files (exclude all the directories) in a folder?

Say I have a folder with many files and directories (NOT actual filenames, this is like a trash can directory, so the filenames are completely random, and some files are without extension):

dir1/
dir2/
...
dirN/
file1
file2
...
fileM

Now I need to move all the files in this directory into the dir1/. That is, move file1, file2 ... fileM into dir1/. What's the easiest way to do that?

If they are all files with extension then the problem is simple, just mv *.* dir1/. But I don't know what to do if there are files without extensions.

Upvotes: 0

Views: 1015

Answers (4)

Barmar
Barmar

Reputation: 780994

One way:

mv $(find * -maxdepth 0 -type f) dir1

Another:

for file in *; do
if [ -f "$file" ]
then mv "$file" dir1
fi
done

Upvotes: -2

Idriss Neumann
Idriss Neumann

Reputation: 3838

Although find is a good solution, here is another solution using bash only :

for file in *; do [[ -f $file ]] && mv "$file" dir1; done

Upvotes: 1

michas
michas

Reputation: 26545

Bash does not have a direct way to select only regular files. You have to use find for that.

Maybe zsh is also an option for you. With zsh you could simply write

mv *(.) dir1/

Upvotes: 0

user3553031
user3553031

Reputation: 6214

find . -type f -maxdepth 1 -exec mv {} dir1/ \;

Upvotes: 5

Related Questions