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 - 170 results
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
cd ~/Desktop && for FILES in $(ls); do mv $FILES .${FILES}; done
for i in $(find . -iname '*.html'); do sed '/String/d' $i > $i-tmp; mv $i-tmp $i; done
2010-09-21 14:35:18
User: cadu
Functions: find mv sed
Tags: sed find
-3

Search in all html files and remove the lines that 'String' is found.

sleep 4; xwd >foo.xwd; mv foo.xwd "$(dd skip=100 if=foo.xwd bs=1 count=256 2>/dev/null | egrep -ao '^[[:print:]]+' | tr / :).xwd"
2010-09-19 08:03:02
User: hackerb9
Functions: mv sleep
3

In general, this is actually not better than the "scrot -d4" command I'm listing it as an alternative to, so please don't vote it down for that. I'm adding this command because xwd (X window dumper) comes with X11, so it is already installed on your machine, whereas scrot probably is not. I've found xwd handy on boxen that I don't want to (or am not allowed to) install packages on.

NOTE: The dd junk for renaming the file is completely optional. I just did that for fun and because it's interesting that xwd embeds the window title in its metadata. I probably should have just parsed the output from file(1) instead of cutting it out with dd(1), but this was more fun and less error prone.

NOTE2: Many programs don't know what to do with an xwd format image file. You can convert it to something normal using NetPBM's xwdtopnm(1) or ImageMagick's convert(1). For example, this would work: "xwd | convert fd:0 foo.jpg". Of course, if you have ImageMagick already installed, you'd probably use import(1) instead of xwd.

NOTE3: Xwd files can be viewed using the X Window UnDumper: "xwud <foo.xwd". ImageMagick and The GIMP can also read .xwd files. Strangely, eog(1) cannot.

NOTE4: The sleep is not strictly necessary, I put it in there so that one has time to raise the window above any others before clicking on it.

ram() { for i in /tmp /altroot;do mount -t tmpfs tmpfs $i;done&& for i in /var /root /etc $HOME; do find -d $i |cpio -pdmv /tmp&& mount -t tmpfs tmpfs $i&& mv -v /tmp$i/* $i&& rm -vrf /tmp$i ; done ;} usage: (in rc sequence) ram
2010-08-31 08:25:55
User: argv
Functions: cpio find mount mv rm
4

if you use disk-based swap then it can defeat the purpose of this function.

declare -i i=0 ; for file in * ; do i=$[$i+1] ; mv "$file" $i; done
declare -i i; i=0; for file in *; do i=`expr $i+1`; mv "$file" $i; done;
2010-08-26 12:24:38
User: themiurgo
Functions: file mv
-5

Renames files in a directory to incremental numbers, following alphabetic order. The command does not maintain extensions.

sed 's/^[ \t]*//;s/[ \t]*$//' < <file> > <file>.out; mv <file>.out <file>
sed 's/^[ \t]*//' < <file> > <file>.out; mv <file>.out <file>