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 - 178 results
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
for i in *; do j=`echo $i | cut -d "-" -f1`; j=$j; mv $i $j; done
2011-03-06 11:58:37
User: jaimerosario
Functions: cut mv
-1

Renames files eliminating suffix, in this case everything after "-" is cutted. Just change "-" with the character you need.

mv .git .. && rm -rf * && mv ../.git . && mv .git/* . && rmdir .git && git config --bool core.bare true
2011-02-28 17:58:14
User: dinomite
Functions: mv rm rmdir
1

If you want to turn a Git repo into the origin that folks can push to, you should make it a bare repository. See: http://stackoverflow.com/questions/2199897/git-convert-normal-to-bare-repository

for i in *; do mv -vi "$i" "`echo "$i"|sed y/????????????????????????/AAAAEEIOOUUCaaaaeeioouuc/`"; done; sync
equery belongs $( which mv )
folder=0;mkdir $folder; while find -maxdepth 1 -type f -exec mv "{}" $folder \; -quit ; do if [ $( ls $folder | wc -l ) -ge 100 ]; then folder=$(( $folder + 1 )); mkdir $folder; fi ; done
2011-02-11 21:28:01
User: Juluan
Functions: find ls mkdir mv wc
-1

If you have a folder with thousand of files and want to have many folder with only 100 file per folder, run this.

It will create 0/,1/ etc and put 100 file inside each one.

But find will return true even if it don't find anything ...

exec 3<&0; ls -1N | while read a; do echo "Rename file: $a"; read -e -i "$a" -p "To: " b <&3 ; [ "$a" == "$b" ] || mv -vi "$a" "$b"; done
function expand-tabs() { expand -t 8 "$1" > "$1.expanded"; mv -f "$1.expanded" "$1"; }
2011-01-28 16:03:37
Functions: expand mv
-4

I don't like TABs in sources files because in case of mixture of TABs and spaces they looks in different editors. Even worse mixing TABs and spaces could be a problem when you use Python.

IFS=$'\n'; i=1; ls -lt *mp3 | cut -d ":" -f2 | cut -d " " -f2- | while read f; do mv "$f" $(echo "$i"."$f"); ((i++)); done
2011-01-22 00:21:12
User: m1cawber
Functions: cut echo ls mv read
0

i use this after ripping internet radio streams to number the files as they originally played (even though streamripper can do this with -q).

to number other types of files, or all files, just change the *mp3. to rename directories only you could use

... ls -lt | grep ^d | cut -d ":" -f2 | cut -d " " -f2- | while read ...

xargs -n 2 mv < file_with_colums_of_names
2010-12-27 18:06:15
User: Juluan
Functions: mv xargs
9

Maybe simpler, but again, don't know how it will work with space in filename.

flipf(){ if [ -f "$1" -a -f "$2" ]; then mv "$1" "$1.$$" && mv "$2" "$1" && mv "$1.$$" "$2" || echo "$!"; else echo "Missing a file: $!"; fi; }
find . ! -name "." -print0 | xargs -0 -I '{}' mv -n '{}' ..; rmdir "$PWD"
2010-12-15 22:12:06
User: bartonski
Functions: find mv rmdir xargs
1

Robust means of moving all files up by a directory. Will handle dot files, filenames containing spaces, and filenames with almost any printable characters. Will not handle filenames containing a single-quote (but if you are moving those, it's time to go yell at whoever created them in the first place).

for f in *.txt;do mv ${f%txt}{txt,md}; done
for f in *.txt; do mv $f `basename $f .txt`.md; done;
2010-12-09 18:29:17
User: vranx
Functions: mv
Tags: batch rename
2

Batch rename extension of all files in a folder, in the example from .txt to .md

for i in ???.jpg; do mv $i $(printf %04d $(basename $i .jpg) ).jpg ; done
2010-11-18 23:48:41
User: carlesso
Functions: basename mv printf
Tags: rename cp printf
5

Useful if you have a list of images called 1 2 3 4 and so on, you can adapt it to rewrite it as 4 (in this example) 0-padded number.

mv -i something.conf{,~} && sed "/regexp/s/^/#/" < something.conf~ > something.conf
mv -- -filename filename
mv ./-filename filename
2010-11-17 12:49:28
User: giacomorizzo
Functions: mv
1

If you don't escape the - of the filename, you will get the command interpreting it as a parameter, returning (in the best case) an error.

for i in ./*.$1; do mv "$i" `echo $i | tr ' ' '_'`; done for i in ./*.$1; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done for i in ./*.$1; do mv "$i" `echo $i | tr '-' '_'`; done for i in ./*.$1; do mv "$i" `echo $i | tr -s '_' `; done
mv -n * ../; cd ..; rmdir $OLDPWD
2010-11-12 20:22:50
User: kevingranade
Functions: cd mv rmdir
Tags: mv rmdir
0

Avoid clobbering files by either overwriting due to name collisions or by assuming the command worked and deleting the target directory.

mv * .[0-9a-Z]* ../; cd ..; rm -r $OLDPWD
OFFS=30;LZ=6;FF=$(printf %%0%dd $LZ);for F in *.jpg;do NF="${F%.jpg}";NF="${NF/#+(0)/}";NF=$[NF+OFFS];NF="$(printf $FF $NF)".jpg;if [ "$F" != "$NF" ];then mv -iv "$F" "$NF";fi;done
2010-11-08 22:48:56
Functions: mv printf
2

When you have different digital cameras, different people, friends and you want to merge all those pictures together, then you get files with same names or files with 3 and 4 digit numbers etc. The result is a mess if you copy it together into one directory.

But if you can add an offset to the picture number and set the number of leading zeros in the file name's number then you can manage.

OFFS != 0 and LZ the same as the files currently have is not supported. Or left as an exercise, hoho ;)

I love NF="${NF/#+(0)/}",it looks like a magic bash spell.

for z in */*.pdf; do gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$z new" -c .setpdfwrite -f "$z" mv "$z new" "$z"; done
for f in * ; do mv "$f" $( echo $f | tr ' ' '-' ) ; done