Using xargs with mv and mkdir command in Linux
Solution 1
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
Solution 2
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?
Solution 3
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.
Related videos on Youtube
PeanutsMonkey
Updated on September 18, 2022Comments
-
PeanutsMonkey over 1 year
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 triedmkdir test | xargs -i mv test.text {}
. It creates the directory, but it does not move the filetest.txt
into it after it has been created. -
PeanutsMonkey almost 12 years@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 almost 12 yearsSorry am new to the world of
find
commands in the terminal but what does `` do at the end of the command? -
PeanutsMonkey almost 12 yearsThere is a specific criteria to move the files. It is simply to create a directory and then to move specific files to that directory.
-
Rob almost 12 yearsIf you meant the backslash, it stops the semicolon from being interpreted by the shell.
-
richardhsu almost 12 yearsWell 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.
-
ott-- almost 12 years@PeanutsMonkey Of if you mean
'test.*'
, the''
prevent the shell from expanding the*
and leave it as it is forfind
.