Create subdirectory and move files into it

(ls; mkdir subdir; echo subdir) | xargs mv
With this form you dont need to cut out target directory using grep/sed/etc.

By: mechmind
2009-11-08 11:40:55

These Might Interest You

  • if you have less free inode space alert in your monitoring app you can use this command to find the any directory with too many files and subdirectory . Show Sample Output

    find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n
    ara4sh · 2017-09-06 05:02:28 0

  • -5
    ps -ef | grep pmon
    Sriregati · 2009-10-10 03:13:46 0
  • This commando copies the file (which must reside in the current directory) to //<server>/<share-name>/<subdirectory>/<file> through the CIFS protocol (Samba share or Windows Share). It doesn't require you to mount the filesystem first. --directory "<subdirectory>" may be omitted in order to copy the file the the root of the share. The "%password" part may also be omitted. If doing so, smbclient will ask for the password interactively. To copy a file from a Windows/Samba share, change "put" for "get". smbclient --user=user%password --directory "&lt;subdirectory&gt;" --command "get &lt;file&gt;" //&lt;server&gt;/&lt;share-name&gt; Show Sample Output

    smbclient --user=user%password --directory "<subdirectory>" --command "put <file>" //<server>/<share-name>
    quitaiskiluisf · 2018-05-12 19:18:42 0
  • These days, most software distributed in tar files will just contain a directory at the top level, but some tar files don't have this and can leave you with a mess of files in the current folder if you blindly execute tar zxvf something.tar.gz This command can help you clean up after such a mistake. However, note that this has the potential to do bad things if someone has been *really* nasty with filenames.

    tar ztf tar-lacking-subdirectory.tar.gz | xargs rm
    mulad · 2009-02-19 00:34:09 0

What Others Think

why not use the && instead of ; ?? it ll check if the previous command was successfull , if yes then execute the next one : (ls && mkdir subdir && echo subdir) | xargs mv
eastwind · 449 weeks and 1 day ago
FYI, you won't move all the files if xargs has to invoke mv more than once. Just a pitfall to be aware of. Neat command!
bwoodacre · 449 weeks and 1 day ago
you could just: mkdir subdir; mv * subdir It will throw an error because you can't move subdir to subdir but will still work.
sharfah · 449 weeks ago
To handle filenames with spaces in, and allow for xargs invoking 'mv' more than once: (ls -1 && mkdir "$1") | xargs -d'\n' mv -t "$1"
pysquared · 449 weeks ago
I really wouldn't use && for this. What happens if the mkdir command fails in this snippet: (ls && mkdir subdir && echo subdir) | xargs mv The output of ls alone will be piped to mv, which will try to move everything into whatever happens to be last in the list (possibly overwriting it). Using semicolons means that the 'echo subdir' command still gets called, so mv will try to move into a non-existent subdirectory (which is much less dangerous).
stevelosh · 448 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: