TCM
TCM

Reputation: 13

exclude file extension on shell script

Currently I have a shell script which works but I need more from it. (I'm terrible with shell...)

Right now, it moves files of the extension type txt and csv to a specific directory. I also need it to then move all other files which are not txt and csv to a different directory for archiving.

Is there a simple approach which after running:

mv *.txt work
mv *.csv work

Will then allow me to:

mv *.*

Excluding txt and csv though? I appreciate any help/input.

Upvotes: 1

Views: 541

Answers (3)

Ali ISSA
Ali ISSA

Reputation: 408

mv *.txt *.csv a_directory
mv * other_directory

Upvotes: 0

mrfred
mrfred

Reputation: 631

GreenGiant's answer makes a lot of sense if you are trying to move those files AFTER you have moved all the files you don't want moved there. However, suppose you wanted the txt and csv files to stay in the current directory. The following solution covers that use case using find.

find . -not -path "." -not -name "*.txt" -not -name "*.csv" -exec mv {} work \;

This looks more complicated, but is more useful in certain situations.

Upvotes: 1

GreenGiant
GreenGiant

Reputation: 5236

Sounds like you almost have your answer. Try:

mv *.txt work
mv *.csv work
mv * archive

This works because, by the time it reaches the last line, all the txt and csv files have already been moved.

Upvotes: 1

Related Questions