LinuxBasics.org

The community that helps people to run Linux

rss

$ ( \ls; echo dir; mkdir dir ) | xargs mv

In my daily mail from UGU.com, there was the commandline above given as a tip for moving files into a newly created subdirectory.

Say what?

$ ( \ls; echo dir; mkdir dir ) | xargs mv

Ok, the ‘$’ is the prompt. ‘(’ and ‘)’ group commands like back then in the algebra-class and runs them in a new shell-environment. ‘\ls’ is the ls-command. It is escaped with the backslash in order to prevent alias-expansion. We don’t want color-codes in the argument-list. The filenames will be one on each line, just plain filenames. Nothing else.

‘echo dir’ prints the name of the new directory (dir) to another line. ‘mkdir dir’ creates the new sudirectory with the name dir.

The pipe-character ‘|’ passes the complete list of files and the following directory-name to another program, which inthis case is ‘xargs’.

‘xargs’ takes the list, pastes them together in to a long argument-list, which it passed to its own argument, which is in this case the command ‘mv’.

‘mv’ expects the last argument to be the destination for the files it has to move. Due to the layout of the command, the name of the newly created directory is always at the end of the list.

But...

The line will fail, if there are so many files (with long names) that xargs has to break the arguments up into two (or more) commands. ‘mv –target-directory=dir’ would fit better after ‘xargs’, making the line read:

$ mkdir dir && ( \ls; mkdir dir ) | xargs mv --target-directory=dir

Anyway, nice trick!


Created by stwaidele


Copyright (c) by the authors.
Prior to editing, authors agreed to license their contributions by the terms of the GPL.
See our licensing page for details.


Linux® is a registered trademark of Linus Torvalds.


 
  tutorials/advanced/realworld/ls_echo_dir_mkdir_dir_xargs_mv.txt · Last modified: 2008/07/20 21:08

LinuxBasics.org

Start Linux-Course Tutorials Linux Links Security Blog Forum E-mail List Search Online Chat

Site-Info

Help Get in Touch Making of LBo

Wiki-Control

Powered by

Linux Apache DokuWiki Mailman RUTE ht://Dig