Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using mv from sorted by
Terminal - Commands using mv - 176 results
for FILE in `ls -1`; do if [ -L "$FILE" ]; then cp $(readlink "$FILE") ${FILE}_rf; rm -f $FILE; mv ${FILE}_rf "$FILE"; fi; done
find . -iname *.java -type f -exec bash -c "iconv -f WINDOWS-1252 -t UTF-8 {} > {}.tmp " \; -exec mv {}.tmp {} \;
for i in *ext; do mv $i ${i%.ext}; done
2011-11-13 03:58:08
User: paulochf
Functions: mv
-3

For those files in current folder that would be shown in `ls *ext`, for some extension ext, move/rename that file removing the .ext suffix from the file name.

It uses Bash's parameter substitution, as seen in

http://tldp.org/LDP/abs/html/parameter-substitution.html#PCTPATREF

(for analog use in prefix, see http://tldp.org/LDP/abs/html/parameter-substitution.html#PSOREX2 )

patch originalfile -i my.patch -o newfile; mv newfile originalfile
2011-10-23 08:51:36
User: anhpht
Functions: mv patch
1

diff originalfile updatedfile > my.patch

find . -iname pom.xml -type f -exec bash -c "cat {} | sed s/1\.0\.46\-SNAPSHOT/1\.0\.48\-SNAPSHOT/g > {}.tmp " \; -exec mv {}.tmp {} \;
2011-10-05 18:57:12
User: miguelbaldi
Functions: bash find mv
0

When a large maven release goes wrong, by deploying just some of the artifacts letting others behind, some projects got wrong SNAPSHOT versions. This command comes to help!

Tip: replace sed's regex by your version numbers

find ./ -type f -exec mv {} . \;
for file in *.mp4; do mv "$file" "${file%.*} [$(cksfv -b -q "$file" | egrep -o "\b[A-F0-9]{8}\b$")].${file#*.}"; done
mv -b old_file_name new_and_already_existent_file_name
2011-09-08 23:57:15
User: ztank1013
Functions: mv
Tags: backup mv cp safe
5

Sometimes in a hurry you may move or copy a file using an already existent file name. If you aliased the cp and mv command with the -i option you are prompted for a confirmation before overwriting but if your aliases aren't there you will loose the target file!

The -b option will force the mv command to check if the destination file already exists and if it is already there a backup copy with an ending ~ is created.

c=blue;convert -size 50x50 xc:$c $c.png; for i in red green yellow; do convert $c.png -background $i -rotate 20 $i.png; rm $c".png"; c=$i; done; mv $i".png" logo.png; display logo.png
2011-08-21 15:59:11
User: Funatiker
Functions: mv rm
Tags: ImageMagick
0

Deletes unneeded files after every step and allows to use a color other than yellow at the last position.

IFS=?" ; for i in * ; do mv -v $i `echo $i|tr ???????????????????\ aaaeeiooAAAEEIOOOcC_` ; done
jpegtran -optimize -outfile temp.jpg <JPEG> && mv temp.jpg "$_"
2011-07-24 08:55:46
User: h3xx
Functions: mv
Tags: jpeg
0

You can also save EXIF information by copying it to temp.jpg:

jpegtran -optimize -outfile temp.jpg <JPEG> && jhead -te temp.jpg "$_" && mv temp.jpg "$_"
find . -iname "*.mp3" -type f -print0 | xargs -0 -I '{}' mv {} /new/path/to/mp3/{}
2011-07-18 05:28:32
User: paulochf
Functions: find mv xargs
2

Use case: folder with flac files with tree structure ../artist/album/number-title.flac

1) convert flac->mp3 in the same folder: http://www.commandlinefu.com/commands/view/6341/convert-all-.flac-from-a-folder-subtree-in-192kb-mp3

2) search for mp3 files and recreate tree structure to another path: http://www.commandlinefu.com/commands/view/8853/copy-selected-folder-found-recursively-under-src-retaining-the-structure

3) move all mp3 files to that new folder: this command

for file in *; do mv -v "$file" "$(sed 's/ //g' <(echo $file))"; done
2011-07-10 21:08:31
User: laebshade
Functions: file mv
1

This is a better version, as it does no command piping, uses for instead of while loops, which allows for a list of files in the current working directory to be natively processed. It also uses the -v/verbose option with mv to let you know what the command is doing.

While the command does exactly the same in a better way, I would modify the sed option to replace spaces with underscores instead, or dashes.

Please note that you'll receive errors with this command as it tries to rename files that don't even have spaces.

This is an alternative to: http://www.commandlinefu.com/commands/view/8761/renames-all-files-in-the-current-directory-such-that-the-new-file-contains-no-space-characters.

find ./ $1 -name "* *" | while read a ; do mv "${a}" "${a//\ /_}" ; done
rf() { for i in "$@"; do mv "$i" "$(pwgen 8 1).${i##*.}"; done }
2011-06-22 07:45:23
User: flatcap
Functions: mv
1

Give files a random name (don't ask why :-)

The function will rename files but maintain their extensions.

BUG: If a file doesn't have an extension it will end up with a dot at the end of the name.

The parameter '8' for pwgen controls the length of filenames - eight random characters.

mv file.png $( mktemp -u | cut -d'.' -f2 ).png
find . | grep ".*\[[Church|CPYAF].*" | while read f; do mv "$f" ../emails;done
find . -name '*.txt' -exec mv {} {}.sh \ ;
2011-06-02 23:47:19
Functions: find mv
-1

You can replace "." with your folder

for i in ./*foo*;do mv -- "$i" "${i//foo/bar}";done
2011-04-30 14:26:05
User: RanyAlbeg
Functions: mv
8

That is an alternative to command 8368.

Command 8368 is EXTREMELY NOT clever.

1) Will break also for files with spaces AND new lines in them AND for an empty expansion of the glob '*'

2) For making such a simple task it uses two pipes, thus forking.

3) xargs(1) is dangerous (broken) when processing filenames that are not NUL-terminated.

4) ls shows you a representation of files. They are NOT file names (for simple names, they mostly happen to be equivalent). Do NOT try to parse it.

Why? see this :http://mywiki.wooledge.org/ParsingLs

Recursive version:

find . -depth -name "*foo*" -exec bash -c 'for f; do base=${f##*/}; mv -- "$f" "${f%/*}/${base//foo/bar}"; done' _ {} +
for f in *; do mv "$f" "${f/foo/bar}"; done
2011-04-30 09:53:20
User: uwe
Functions: mv
-1

without sed, but has no problems with files with spaces or other critical characters

for x in *;do mv "$x" "`echo $x|tr [A-Z] [a-z]`";done
for i in {6..1} ; do for f in *.$i.gz ; do mv "$f" "${f/.$i.gz}".$((i+1)).gz 2> /dev/null ; done; done
2011-04-18 11:44:36
User: Neo23x0
Functions: mv
1

Rotates log files with "gz"-extension in a directory for 7 days and enumerates the number in file name.

i.e.: logfile.1.gz > logfile.2.gz

I needed this line due to the limitations on AIX Unix systems which do not ship with the rename command.

find <folder> -type f -name '*.jpg' -exec bash -c 'ext="${0##*.}";path="$(dirname "$0")";name="$(basename "$0"|sed "s/.jpg//")";folder="$(dirname "$0"|tr / \\n |tail -1)";new="${path}/${name}_${folder}.${ext}"; mv "$0" "${new}"' {} \;
2011-04-08 20:13:15
Functions: bash find mv
0

find pictures recursively in a specified folder and renames the file name to originalname_containingfoldername.jpg

find . -type f -name \*.php | while IFS="" read i; do expand -t4 "$i" > "$i-"; mv "$i-" "$i"; done
2011-04-08 12:53:14
User: flatcap
Functions: expand find mv read
-4

Recursively find php files and replace tab characters with spaces.

Options:

"\*.php" -- replace this with the files you wish to find

"expand" -- replace tabs with spaces (use "unexpand" to replace spaces with tabs)

"-t4" -- tabs represent 4 spaces

Note: The IFS="" in the middle is to prevent 'read' from eating leading/trailing whitespace in filenames.

for i in $(find . -type f); do mv "$i" "$(echo $i|tr A-Z a-z)"; done