39

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?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Conner
  • 723
  • 1
  • 8
  • 17
  • 1
    Please ask your sysadmin or ask at [Super User](http://superuser.com/tour). Stack Overflow is a question and answer site for professional and enthusiast programmers. – Cyrus Dec 22 '16 at 06:37
  • 2
    @Cyrus: You conflate who vs. what. StackOverflow is _not_ a site for "professional and enthusiast programmers", it is a site for asking questions about programming, among related topics, though programmers of the sort you mention are probably heavily represented. – Richard Dec 22 '16 at 19:13
  • 1
    @Richard: Source: http://stackoverflow.com/tour – Cyrus Dec 22 '16 at 19:19
  • 12
    @Cyrus: I disagree with the wording there as well, and, indeed, any wording that would exclude people who do not self-identify in the way you suggest. The wording "for professional and enthusiast programmers", especially combined with "ask your sysadmin" is dismissive. It says "_you_ are not welcome here", rather than "your question is better suited to another forum". – Richard Dec 22 '16 at 19:34

5 Answers5

60

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)
Richard
  • 56,349
  • 34
  • 180
  • 251
  • 4
    That's the ticket! Thank you Richard! Learning one step at a time. – Conner Dec 22 '16 at 06:36
  • 1
    Glad to help, @Kronus. If this was the best answer to your question, or you feel your question's been answered satisfactorily, please click the big check mark near the up/down vote arrows to accept the answer. – Richard Dec 22 '16 at 06:37
13

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

Amit
  • 30,756
  • 6
  • 57
  • 88
  • I was aware of this, hence adding the '(more files)'. I was not explicit enough though. Thank you for sharing! – Conner Dec 22 '16 at 06:40
  • 1
    @Kronus: 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. It's great to have aliases for them in your .bashrc file. – codeforester Dec 22 '16 at 06:59
  • @codeforester Thanks for sharing this information. Its useful for the people who are new and don't know the in and out of the commands :). – Amit Dec 22 '16 at 07:06
4

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.

2

You can move multiple files to a specific directory by using mv command. In your scenario it can be done by,

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

The point you must note is that the last entry is the destination and rest everything except mv is source.

One another scenario is that the destination is not present in our directory,then we must opt for absolute path in place of vehicles/.

Note: Absolute path always starts from / ,which means we are traversing from root directory.

0

I have written a small bash script that will move multiple files(matched using pattern) present in multiple directories(matched using pattern) to a single location using mv and find command in bash

#!/bin/bash

for i in $(find /path/info/*/*.fna -type f) # find files and return their path

do


        mv -iv $i -t ~/path/to/destination/directory # move files 

done
  1. $() is for command substitution(in other words it expand the expression inside it)
  2. /*/ wild card for matching any directory, you can replace this with any wild card expression
  3. *.fna is for finding any file with.fna extension
  4. -type f is for getting the full path info of the located file
  5. -i in mv is for prompt before overwrite( extra caution in case the wild card exp was wrong)
  6. -v for verbose
  7. -t for destination

NOTE: the above flags are not mandatory

Hope this helps

Sujith
  • 1
  • 1