I know you can do it with a find
, but is there a way to send the output of ls
to mv
in the unix command line?
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- softlinks atime and mtime modification
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- Compile and build with single command line Java (L
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
else, it's the find solution by sybreon, and as suggested NOT the green mv ls solution.
Not exactly sure what you're trying to achieve here, but here's one possibility:
The "xargs" part is the important piece everything else is just setup. The effect of this is to take everything that "ls" outputs and add a ".txt" extension to it.
Something like this could also be achieved by:
I sort of like the second way better. I can NEVER remember how xargs works without reading the man page or going to my "cute tricks" file.
Hope this helps.
ls
is a tool used to DISPLAY some statistics about filenames in a directory.It is not a tool you should use to enumerate them and pass them to another tool for using it there. Parsing
ls
is almost always the wrong thing to do, and it is bugged in many ways.For a detailed document on the badness of parsing ls, which you should really go read, check out: http://mywiki.wooledge.org/ParsingLs
Instead, you should use either globs or find, depending on what exactly you're trying to achieve:
The main source of badness of parsing
ls
is that ls dumps all filenames into a single string of output, and there is no way to tell the filenames apart from there. For all you know, the entirels
output could be one single filename!The secondary source of badness of parsing
ls
comes from the broken way in which half the world uses bash. They thinkfor
magically does what they would like it to do when they do something like:for
is a bash builtin that iterates over arguments. And$(ls)
takes the output ofls
and cuts it apart into arguments wherever there arespaces
,newlines
ortabs
. Which basically means, you're iterating over words, not over filenames. Even worse, you're asking bask to take each of those mutilated filename words and then treat them as globs that may match filenames in the current directory. So if you have a filename which contains a word which happens to be a glob that matches other filenames in the current directory, that word will disappear and all those matching filenames will appear in its stead!None of the answers so far are safe for filenames with spaces in them. Try this:
You can of course use any glob pattern you like in place of
*
.One way is with backticks:
Edit: however, this is fragile and not recommended -- see @lhunath's asnwer for detailed explanations and recommendations.
Backticks work well, as others have suggested. See xargs, too. And for really complicated stuff, pipe it into sed, make the list of commands you want, then run it again with the output of sed piped into sh.
Here's an example with find, but it works fine with ls, too:
http://github.com/DonBranson/scripts/blob/f09d24629ab6eb3ce509d4d3078818430306b063/jarfinder.sh