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 - 174 results
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>
sed 's/[ \t]*$//' < <file> > <file>.out; mv <file>.out <file>
for i in $(find . -name *replaceme*);do mv "$i" "${i//replaceme/withme}"; done
2010-08-19 03:22:15
User: lefnire
Functions: find mv
Tags: bash
-6

rename is a great command, but can't get it to work on mac.

for i in *; do mv $i prependtext$i; done
ls | while read -r FILE; do mv -v "$FILE" `echo "prependtext$FILE" `; done
2010-08-14 14:19:18
User: IgnitionWeb
Functions: ls mv read
Tags: echo mv prepen
-4

Prepends all directory items with "prependtext"

ls | while read -r FILE; do mv -v "$FILE" `echo $FILE | tr -d ' '`; done
2010-08-14 14:10:48
User: IgnitionWeb
Functions: ls mv read tr
Tags: space echo while tr
-3

all files in the directory get moved, in doing so the new name of the file is the original name with out spaces (using translate command)

file /music/dir/* | grep -v 44.1 | sed 's/:.*//g' | grep .mp3 | { while IFS= read; do filebak="\"$REPLY.original\""; file="\"$REPLY\""; mv $file $filebak; sox -t mp3 $filebak $file rate 44k; done; };
2010-08-12 21:53:28
User: IgnitionWeb
Functions: file grep mv sed
Tags: mp3 sox resample
-2

This heavy one liner gets all the files in the "/music/dir/" directory and filters for non 44.1 mp3 files. After doing this it passes the names to sox in-order to re-sample those files. The original files are left just in case.

find ./* -mtime +60 -exec mv {} storeFolder \;
read -p "enter url:" a ; w3m -dump $a > /dev/shm/e1q ; less /dev/shm/e1q ; read -p "save file as text (y/n)?" b ; if [ $b = "y" ] ; then read -p "enter path with filename:" c && touch $(eval echo "$c") ; mv /dev/shm/e1q $(eval echo "$c") ; fi ; echo DONE
2010-07-13 22:36:38
User: LinuxMan
Functions: c++ echo eval less mv read touch
0

Thanks th John_W for suggesting the fix allowing ~/ to be used when saving a directory.

directions:

Type in a url, it will show a preview of what the file will look like when saved, then asks if you want to save the preview and where you want to save it. Great for grabbing the latest commandlinefu commands without a full web browser or even a GUI. Requires: w3m

variable="foo" && sed 's/bar/'$variable'/g' $variable.conf >> $variable.temp && sed '1,5d' $variable.temp && mv $variable.temp $variable.conf
2010-07-09 22:12:51
User: jdorfman
Functions: mv sed
Tags: bash sed nginx
0

I wrote this script to speed up Nginx configs. This (long) one liner can be run via BASH. You will see that we set a variable in bash called 'foo' and the streamline editor (sed) finds 'bar' in 'foo.conf' next it writes that output to a temp file (foo.temp) and removes the first 5 lines (that aren't needed in this case) & lastly it moves (overwrites) foo.temp to foo.conf

for x in *.dat;do sort -k 3 $x >tmp && mv -f tmp $x;done
2010-07-07 07:57:37
User: rajarshi
Functions: mv sort
Tags: sorting
-2

We normally get tasks in which one has to sort a data file according to some column. For a single file say foo, we would use

sort -k 3 foo >tmp && tmp foo

The for loop is useful when we have to do it on a number of files.

find ~ -maxdepth 20 -type f -size -16M -print > t; for ((i=$(wc -l < t); i>0; i--)) do a=$(sed -n ${i}p < t); mv "$a" /dev/shm/d; mv /dev/shm/d "$a"; echo $i; done; echo DONE; rm t
2010-07-07 04:29:22
User: LinuxMan
Functions: echo find mv rm sed wc
2

Thanks to flatcap for optimizing this command.

This command takes advantage of the ext4 filesystem's resistance to fragmentation.

By using this command, files that were previously fragmented will be copied / deleted / pasted essentially giving the filesystem another chance at saving the file contiguously. ( unlike FAT / NTFS, the *nix filesystem always try to save a file without fragmenting it )

My command only effects the home directory and only those files with your R/W (read / write ) permissions.

There are two issues with this command:

1. it really won't help, it works, but linux doesn't suffer much (if any ) fragmentation and even fragmented files have fast I/O

2. it doesn't discriminate between fragmented and non-fragmented files, so a large ~/ directory with no fragments will take almost as long as an equally sized fragmented ~/ directory

The benefits i managed to work into the command:

1. it only defragments files under 16mb, because a large file with fragments isn't as noticeable as a small file that's fragmented, and copy/ delete/ paste of large files would take too long

2. it gives a nice countdown in the terminal so you know how far how much progress is being made and just like other defragmenters you can stop at any time ( use ctrl+c )

3. fast! i can defrag my ~/ directory in 11 seconds thanks to the ramdrive powering the command's temporary storage

bottom line:

1. its only an experiment, safe ( i've used it several times for testing ), but probably not very effective ( unless you somehow have a fragmentation problem on linux ). might be a placebo for recent windows converts looking for a defrag utility on linux and won't accept no for an answer

2. it's my first commandlinefu command

HTMLTEXT=$( curl -s http://www.page.de/test.html > /tmp/new.html ; diff /tmp/new.html /tmp/old.html ); if [ "x$HTMLTEXT" != x ] ; then echo $HTMLTEXT | mail -s "Page has changed." mail@mail.de ; fi ; mv /tmp/new.html /tmp/old.html
2010-07-04 21:45:37
User: Emzy
Functions: diff echo mail mv
2

Checks if a web page has changed. Put it into cron to check periodically.

Change http://www.page.de/test.html and mail@mail.de for your needs.

sudo find . -maxdepth 1 -cnewer olderFilesNameToMove -and ! -cnewer newerFileNameToMove -exec mv -v {} /newDirectory/ \;
2010-06-30 20:40:30
User: javamaniac
Functions: find mv sudo
2

In a folder with many files and folders, you want to move all files where the date is >= the file olderFilesNameToMove and

for n in * ; do mv $n `echo $n | tr '[:lower:]' '[:upper:]'`; done
2010-06-25 19:20:04
User: max_allan
Functions: mv tr
1

Simple bash/ksh/sh command to rename all files from lower to upper case. If you want to do other stuff you can change the tr command to a sed or awk... and/or change mv to cp....

for i in somefiles*.png ; do echo "$i" ; N=$(stat -c %Y $i); mv -i $i $N.png; done
2010-06-01 19:28:05
User: sufoo
Functions: echo mv stat
0

This renames a pattern matched bunch of files by their last modified time.

rename by timestamp

rename by time created

rename by time modified

lynx -dump -listonly 'url' | grep -oe 'http://.*\.ogg' > 11 ; vlc 11 ; mv 11 /dev/null
shopt -s extglob; for f in *.ttf *.TTF; do g=$(showttf "$f" 2>/dev/null | grep -A1 "language=0.*FullName" | tail -1 | rev | cut -f1 | rev); g=${g##+( )}; mv -i "$f" "$g".ttf; done
2

Just a quick hack to give reasonable filenames to TrueType and OpenType fonts.

I'd accumulated a big bunch of bizarrely and inconsistently named font files in my ~/.fonts directory. I wanted to copy some, but not all, of them over to my new machine, but I had no idea what many of them were. This script renames .ttf files based on the name embedded inside the font. It will also work for .otf files, but make sure you change the mv part so it gives them the proper extension.

REQUIREMENTS: Bash (for extended pattern globbing), showttf (Debian has it in the fontforge-extras package), GNU grep (for context), and rev (because it's hilarious).

BUGS: Well, like I said, this is a quick hack. It grew piece by piece on the command line. I only needed to do this once and spent hardly any time on it, so it's a bit goofy. For example, I find 'rev | cut -f1 | rev' pleasantly amusing --- it seems so clearly wrong, and yet it works to print the last argument. I think flexibility in expressiveness like this is part of the beauty of Unix shell scripting. One-off tasks can be be written quickly, built-up as a person is "thinking aloud" at the command line. That's why Unix is such a huge boost to productivity: it allows each person to think their own way instead of enforcing some "right way".

On a tangent: One of the things I wish commandlinefu would show is the command line HISTORY of the person as they developed the script. I think it's that conversation between programmer and computer, as the pipeline is built piece-by-piece, that is the more valuable lesson than any canned script.

mv ubuntu-10.04-rc-desktop-amd64.iso ubuntu-10.04-desktop-amd64.iso; i=http://releases.ubuntu.com/10.04/ubuntu-10.04-desktop-amd64.iso.zsync; while true; do if wget $i; then zsync $i; date; break; else sleep 30; fi; done
2010-04-29 15:49:43
Functions: mv sleep wget
4

Need to have rc iso pre-downloaded before running command.

inplace() { eval F=\"\$$#\"; "$@" > "$F".new && mv -f "$F".new "$F"; }
2010-04-09 11:36:31
User: inof
Functions: eval mv
1

Some commands (such as sed and perl) have options to support in-place editing of files, but many commands do not. This shell function enables any command to change files in place. See the sample output for many examples.

The function uses plain sh syntax and works with any POSIX shell or derivative, including zsh and bash.

for each in *; do file="$each."; name=${file%%.*}; suffix=${file#*.}; mv "$each" "$(echo $name | rot13)${suffix:+.}${suffix%.}"; done
2010-03-20 16:11:12
User: hfs
Functions: mv
-1

This got a bit complicated, because I had to introduce an additional dot at the end that has to be removed again later.

find -type f -exec mv {} . \;
2010-03-02 07:09:45
User: and3k
Functions: find mv
10

Find every file and move it to current directory.