Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am attempting to create a directory using the command mkdir. However, I would like to move a subset of files into that directory. I understand I can use xargs, however my attempts have failed. For example, I have tried mkdir test | xargs -i mv test.text {}. It creates the directory, but it does not move the file test.txt into it after it has been created.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

mkdir does not output anything, therefore xargs will not do anything useful. I don't understand exactly what you want to do, so you should explain your question better.

"would like to move a subset of files into that directory" -> do files in this subset share one or more traits? If yes, use the find command like this:

find [conditions] -exec mv "{}" dirname \;

If you want to avoid typing the name of the directory twice, or if you are doing this from a script, you can do something like

dirname=test
mkdir $dirname && mv filename $dirname
share|improve this answer
    
@user49740- Thanks. I wasn't aware that mkdir wasn't producing any output. I thought it did as it displays them in standard output i.e. the monitor. The files don't share a specific trait. I am simply attempting to create a directory and then to move files that I need to have moved into the newly created directory. –  PeanutsMonkey Jun 6 '12 at 20:29

I don't think mkdir produces any output to pipe to xargs.

Doing "mkdir test1 test2 testhello3 | grep hello" doesn't produce any output while doing "ls | grep hello" will show me "testhello3".

Can you produce the folders first and then use ls then grep and then xargs to do it?

ls | grep (unique search to get folder name) | xargs -i mv test.txt {}

Maybe more information on criteria of moving files and such would be helpful.

share|improve this answer
    
There is a specific criteria to move the files. It is simply to create a directory and then to move specific files to that directory. –  PeanutsMonkey Jun 6 '12 at 20:31
    
Well I mean if you're making it at the moment and then moving files then you can just do "mkdir foldername && mv file.txt file2.txt file3.txt foldername/." Since you know the foldername and files. –  richardhsu Jun 6 '12 at 20:37

My guess, close to user49740's answer:

mkdir test && find -type f -name 'test.*' -exec mv {} test \;

how many diferent directories do you want to create?

share|improve this answer
    
Sorry am new to the world of find commands in the terminal but what does `` do at the end of the command? –  PeanutsMonkey Jun 6 '12 at 20:30
1  
If you meant the backslash, it stops the semicolon from being interpreted by the shell. –  Rob Jun 6 '12 at 20:34
    
@PeanutsMonkey Of if you mean 'test.*', the '' prevent the shell from expanding the * and leave it as it is for find. –  ott-- Jun 7 '12 at 8:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.