Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Bash: Move multiple different files into same directory

Tags:

As a rather novice Linux user, I can't seem to find how to do this. I am trying to move unique files all in one directory into another directory. Example:

$ ls
vehicle car.txt bicycle.txt airplane.html train.docx (more files)

I want car.txt, bicycle.txt, airplane.html, and train.docx inside vehicle.

Right now I do this by moving the files individually:

$ mv car.txt vehicle
$ mv bicycle.txt vehicle
...

How can I do this in one line?

like image 240
Conner Avatar asked Dec 22 '16 06:12

Conner


People also ask

How do I move multiple files in bash?

Move Multiple Files With the mv Command in LinuxAfter the mv command, type the filenames you want to move and then the directory name. The use of a slash ( / ) after the directory name is optional.

How do I send multiple files to a folder in Linux?

How to move multiple files into a directory. To move multiple files using the mv command pass the names of the files or a pattern followed by the destination. The following example is the same as above but uses pattern matching to move all files with a . txt extension.

How do I move a group of files into a folder in Linux?

The mv (move) command is used to move one or more files or directories from one directory to another directory using terminal in the Linux/Unix operating system. After using the mv command file is copied from source to destination and source file is removed. The mv command is also used to rename the file.


3 Answers

You can do

mv car.txt bicycle.txt vehicle/

(Note that the / above is unnecessary, I include it merely to ensure that vehicle is a directory.)

You can test this as follows:

cd               #Move to home directory
mkdir temp       #Make a temporary directory
touch a b c d    #Make test (empty) files ('touch' also updates the modification date of an existing file to the current time)
ls               #Verify everything is there
mv a b c d temp/ #Move files into temp
ls               #See? They are gone.
ls temp/         #Oh, there they are!
rm -rf temp/     #DESTROY (Be very, very careful with this command)
like image 166
Richard Avatar answered Sep 25 '22 10:09

Richard


Shorthand command to move all .txt file

You can try using a wildcard. In the code below, * will match all the files which have any name ending with .txt or .docx, and move them to the vehicle folder.

mv *.txt *.docx vehicle/ 

If you want to move specific files to a directory

mv car.txt bicycle.txt vehicle/

Edit: As mentioned in a comment, If you are moving files by hand, I suggest using mv -i ... which will warn you in case the destination file already exists, giving you a choice of not overwriting it. Other 'file destroyer' commands like cp & rm too have a -i option

like image 42
Amit Avatar answered Sep 23 '22 10:09

Amit


mv command in linux allow us to move more than one file into another directory. All you have to do is write the name of each file you want to move, seperated by a space.

Following command will help you:

mv car.txt bicycle.txt airplane.html train.docx vehicle

or

mv car.txt bicycle.txt airplane.html train.docx vehicle/

both of them will work.

like image 3
Ankit Kumar Singh Avatar answered Sep 24 '22 10:09

Ankit Kumar Singh