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 - 180 results
mv -iv $FILENAME{,.$(stat -c %Z $FILENAME)}
2014-12-02 13:47:52
User: bunam
Functions: mv stat
Tags: mv
3

FILENAME=nohup.out

mv -iv $FILENAME{,.$(stat -c %Y $FILENAME)}

does it help ?

mv -iv $FILENAME{,.$(stat -c %y $FILENAME | awk '{print $1}')}
2014-12-01 22:41:38
User: pdxdoughnut
Functions: awk mv stat
Tags: mv
3

Note that the -i will not help in a script. Proper error checking is required.

find . -type f -name "*\?*" | while read f;do mv "$f" "${f//[^0-9A-Za-z.\/\(\)\ ]/_}";done
2014-11-28 14:55:27
User: miccaman
Functions: find mv read
Tags: bash find mv
1

replace all "?" characters in filename to underscore

find . -type d -name "*\?*" | while read f;do mv "$f" "${f//[^0-9A-Za-z.\/\(\)\ ]/_}";done
2014-11-28 14:52:46
User: miccaman
Functions: find mv read
Tags: bash find mv
0

rename all dirs with "?" char in name, leave spaces and () in place

locate -0 -i *barthes* | xargs -0 mv -t ~/'Library/Books/Barthes, Roland'
2014-11-16 18:26:35
User: qdrizh
Functions: locate mv xargs
Tags: mv xargs locate
0

Find all books on my systems and move them into folder. The -0 switches are to handle spaces etc. in the filenames.

Why would you need this? Locate uses an index, so it's super quick, and xargs is more elegant than a for loop.

for i in `ls` ; do date=$(identify -format %[exif:DateTime] $i); date=${date//:/-}; date=${date// /_}; mv $i ${date}__$i; done
find . -iname "*.mp4" -print0 | xargs -0 mv --verbose -t /media/backup/
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash; mv git-completion.bash ~/.git-completion.bash; echo "source ~/.git-completion.bash" > ~/.bashrc; source ~/.git-completion.bash
2014-07-22 14:21:00
User: servel333
Functions: echo mv wget
0

Downloads this raw script https://github.com/git/git/blob/master/contrib/completion/git-completion.bash from Github, copies it to your home directory, autoloads it in ~/.bashrc and sources it.

find . -type f -iname '*.flac' | while read i; do mv -- "$i" "$i.tmp"; gst-launch filesrc location="$i.tmp" ! flacdec ! flacenc quality=8 ! filesink location="${i%.tmp}"; rm -- "$i.tmp"; done
2014-07-10 19:21:22
User: qdrizh
Functions: find mv read rm
0

Sometimes I get FLAC files that RhythmBox can't play but VLC can. So I re-encode them using GStreamer at highest compression.

[ ! -d /squashed/usr ] && mkdir -p /squashed/usr/{ro,rw} ; mksquashfs /usr /squashed/usr/usr.sfs.new -b 65536 ; mv /squashed/usr/usr.sfs.new /squashed/usr/usr.sfs ; reboot
2014-05-10 06:01:06
User: mhs
Functions: mkdir mv
4

Periodically run the one-liner above if/when there are significant changes to the files in /usr/

= Before rebooting, add following to /etc/fstab : =

/squashed/usr/usr.sfs /squashed/usr/ro squashfs loop,ro 0 0 usr /usr aufs udba=reval,br:/squashed/usr/rw:/squashed/usr/ro 0 0

No need to delete original /usr/ ! (unless you don't care about recovery). Also AuFS does not work with XFS

for i in $(find . -regex '.*\/C.*\.cpp'); do svn mv `perl -e 'my $s=$ARGV[0]; $s=~m/(.*\/)C(.*)/; print "$s $1$2"' "$i"`; done
find . -mtime +30 -exec mv {} old/ \;
2014-02-09 23:05:41
User: minnmass
Functions: find mv
Tags: bash file
-2

Use find's built-in "exec" option to avoid having to do any weirdness with quoting.

for i in $(find . -mtime +30); do mv $i old/; done
2014-02-05 01:24:45
User: valferon
Functions: find mv
Tags: bash file
-3

Will move in that case every file in the current folder older than 30 days to the "old" folder

Replace "mv $i old/" by any command such as rm / echo to do something different.

for i in xxxx*.mp4; do j=`echo $i | sed 's/ - \([0-9][0-9]\). / S1E\1 - /g'`; mv "$i" "$j"; done
2014-02-01 21:17:33
User: tomtom99
Functions: mv sed
0

Renames all files in the following format

xxxxxx - 34. yyyyy.mp4

to the following format

xxxxxx S1E34 - yyyyy.mp4

mkdir Epub ; mv -v --target-directory=Epub $(fgrep -lr epub *)
IFS=$(echo -en "\n\b"); mkdir Epub; for i in `find . | grep epub`; do echo "epub: $i"; mv -v "$i" Epub; done
for fn in *.epub; do echo mv \"$fn\" \"`echo "$fn" | sed -E 's/\.*\/*(.*)( - )(.*)(\.[^\.]+)$/\3\2\1\4/' | sed -E 's/(.*) ([^ ]+)( - )(.*)/\2, \1\3\4/' `\";done | sh
2013-11-30 05:29:52
User: woohoo
Functions: echo mv sed
0

If you want to test output, run it like this:

for fn in *.epub; do echo mv \"$fn\" \"`echo "$fn" | sed -E 's/\.*\/*(.*)( - )(.*)(\.[^\.]+)$/\3\2\1\4/' | sed -E 's/(.*) ([^ ]+)( - )(.*)/\2, \1\3\4/' `\";done > rename.txt

mv /etc/fstab /etc/fstab.old && mount | awk '{print $1, $3, $5, $6}'| sed s/\(//g|sed s/\)/' 0 0'/g >> /etc/fstab
for i in `ls -r *.png`; do mv $i `printf "%03d.png" $(( 10#${i%.png}+1 ))`; done
for file in $(git ls-files | grep old_name_pattern); do git mv $file $(echo $file | sed -e 's/old_name_pattern/new_name_pattern/'); done
i=1; for f in *.[jJ][pP][gG]; do mv -- "$f" "$( printf "new_name_of_file-%04d.jpg" $i )"; ((i++)); done
2013-10-08 20:12:57
Functions: mv
Tags: batch rename
0

I wrote a script to make it a bit easier and more flexable:

#!/bin/bash

#By GrouchyGaijin

# Hard part (lines 15 and 21) by Vaphell

#Last Updated: 04-Oct-2013 (Friday - yes see what I do on Firday nights) @ 23:37

#Put this in path and call it by typing batchrename in the terminal

#Version 0.3

echo "This script is for batch renaming files."

read -p "Enter the path to the folder containing the files. " path

cd $path

read -p "Enter the file extension. " extension

echo "Press N if you want to use a new name or D if you want to use the date"

read x

if [ "$x" = "D" ]

then

i=1; for f in *; do mv -- "$f" "$( printf "$( date +%d-%b-%Y )-%04d.$extension" $i )"; ((i++)); done

fi

if [ "$x" = "N" ]

then

read -p "Enter the new base name: " new_name

i=1; for f in *; do mv -- "$f" "$( printf "$new_name-%04d.$extension" $i )"; ((i++)); done

fi

#OK S11 - change this to Thunar

nautilus $path

CSS=$HOME/.config/google-chrome/Default/User\ StyleSheets/Custom.css sh -c 'test -f "$CSS.off" && mv "$CSS.off" "$CSS" || mv "$CSS" "$CSS.off"'
2013-10-06 00:38:00
User: Romero
Functions: mv sh
0

A very simple command to toggle Chrome?s default style sheet. It uses the test command to see if the "Custom.css.off" file exists, if so, it will become "Custom.css", and if not, "Custom.css" is moved to "Custom.css.off" Thus, swapping.

This is accomplished with "&&" and "||"

for f in ./*.xls; do mv "$f" "${f%.*}.ods"; done
2013-09-17 01:41:56
User: evilsoup
Functions: mv
Tags: bash Linux
3

An entirely shell-based solution (should work on any bourne-style shell), more portable on relying on the rename command, the exact nature of which varies from distro to distro.

for fil in *.JPG; do datepath="$(identify -verbose $fil | grep DateTimeOri | awk '{print $2"_"$3 }' | sed s%:%_%g)"; mv -v $fil $datepath.jpg; done
2013-08-02 01:42:04
Functions: mv
0

Requires ImageMagick.

Extracts date taken from image and renames it properly.

Based on StackOverflow answer.

for i in * ; do mv $i $[j++].mp3 ; done
2013-07-01 08:14:59
User: yangfeng724
Functions: mv
0

This command will replace spaces in filename with number