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.


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:



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.




Commands using cp from sorted by
Terminal - Commands using cp - 102 results
$ if check4progs cp foo mv bar rsync; then echo "needed progs avail, lets do funky stuff"; else echo "oh oh better abort now"; fi
2015-01-01 16:16:00
User: Xk2c
Functions: cp echo mv

Thanks to the great grml team for this func!

You really should look at their shell configs for further usefull things!


# {{{ check for availability of program(s)

# usage example:

# check4progs [-s,-q,--quiet,--silent] arg [arg .... argn]


# with option given either of:

# -s,-q,--quiet,--silent


# check for available progs but produce no output

check4progs() {

[ -n "${ZSH_VERSION}" ] && emulate -L sh

local RTN=0

local oldifs="${IFS}"

local ARG d found

local VERBOSE=1

case ${1} in

-q | -s | --quiet | --silent)


shift 1





while [ $# -gt 0 ]






for d in $PATH


if [ -x "${d}/${ARG}" ]







# check for availability

if [ ${found} -eq 0 ]


if [ ${VERBOSE} -eq 1 ]


printf "%s: binary not found\n" "${ARG}" >&2





# return non zero, if at least one prog is missing!

return $RTN


# }}}

cp -p file.txt{,.bak}
2014-09-12 13:49:26
User: RedFox
Functions: cp

Use shell expansion to create a back-up copy (file.txt.bak) of a file (file.txt) and keep the original last modified date and time.

for file in ./data/message-snapshots/*.jpg; do cp "$file" /data/digitalcandy/ml/images/; done
2014-06-14 17:26:21
User: ferdous
Functions: cp file
Tags: cp ARG_MAX

helpful when you see something like this:

zsh: argument list too long: cp

cp filename{,.`date +%Y%m%d-%H%M%S`}
2014-02-21 10:21:28
User: techtonik
Functions: cp

This inserts timestamp instead of .bak extension.

find * -regextype posix-extended -regex '.*\.(ext_1|ext_2)' -exec cp {} copy_target_directory \;
cp foo{,bak}
2014-01-22 14:09:42
Functions: cp
Tags: cp

Utilizes shell expansion of {} to give the original filename and a new filename as arguments to `cp`. Can easily be extended to make multiple copies.

ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
cp -p data.json data.yml
(while read fn; do; cp "$fn" $DESTINATION\.; done<filename.txt)
2013-05-05 16:29:51
User: jameskirk
Functions: cp read
Tags: shell

If you want to copy all files listed (with full path) in a text-file (i.e. cmus playlist.pl) to a certain directory use this nice oneliner...

Credits goes to RiffRaff: http://www.programmingforums.org/post242527-2.html

cp data.{json,yaml}
for FILE in *.conf; do cp $FILE{,.`date +%Y%m%dt%M:%H:%S`}; done
sudo cp /usr/share/zoneinfo/Europe/Paris /etc/localtime
echo "template file: ";read tpl;echo "new file(s separated w. space):"; read fl;touch $fl;find $fl -exec cp -ap $tpl "{}" \;
2013-03-08 10:00:36
User: knoppix5
Functions: cp echo find read touch

make a bunch of files with the same permissions, owner, group, and content as a template file

(handy if you have much to do w. .php, .html files or alike)

for i in *RET.zip; do unzip -l "$i"| grep -B 4 XXX | grep RET| sed "s/.\+EPS/EPS/" |xargs -I '{}' cp '{}' out/'{}';done;
git diff --name-only --diff-filter=AMXTCR HEAD~2 HEAD | xargs -l -I{} cp --parents --verbose "{}" target_dir
find /var/www/ -name file -exec cp {}{,.bak} \;
2013-01-27 01:03:28
User: joepd
Functions: cp file find

Let the shell handle the repetition in stead of find :)

find /var/www/ -name file -exec cp {} {}.bak \;
cp -p `ls -l | awk '/Apr 14/ {print $NF}'` /usr/users/backup_dir
edit-notime () { FILE=$1; TMP=`mktemp /tmp/file-XXXXXX`; cp -p $FILE $TMP; $EDITOR $TMP; touch -r $FILE $TMP; cp -p $TMP $FILE; rm -f $TMP; }
2012-10-31 00:54:19
User: jecxjoopenid
Functions: cp rm touch

Copies file to a temporary location, edit and set to real file's time stamp then copy back. Assumes access to /tmp and has $EDITOR, but can be replaced with better values.

cp foo.txt foo.txt.tmp; sed '$ d' foo.txt.tmp > foo.txt; rm -f foo.txt.tmp
2012-09-13 20:57:40
User: kaushalmehra
Functions: cp rm sed
Tags: sed unix

sed '$ d' foo.txt.tmp

...deletes last line from the file

buf () { oldname=$1; if [ "$oldname" != "" ]; then datepart="$(date +%Y-%m-%d).bak"; firstpart=`echo $oldname | cut -d "." -f 1`; newname=`echo $oldname | sed s/$firstpart/$firstpart.$datepart/`; cp -iv ${oldname} ${newname}; fi }
2012-08-15 08:31:44
User: juliushibert
Functions: cp cut sed

Appends the input file with the date format YYYY-MM-DD.bak. Also runs silently if you remove the -v on the cp at the end of the function.

cp -rf srcdir/* destdir
2012-06-25 13:01:03
User: jlaunay
Functions: cp
Tags: cp

As cp is often an alias to cp -i you can use \cp (or cp full path /bin/cp) to use the real cp command instead of its alias

cp $(find /media/ -type f -name "*.wav" -printf "%T@ %h/%f\n" | sort | tail -1 | awk '{ print $2 }') .
2012-06-01 12:45:43
User: hamoid
Functions: awk cp find sort tail

Watch out if you have several USB drives plugged in: it scans the whole /media/ folder !!! You can replace /media/ by the path of a specific USB drive (something like /media/F77A-530B/)

I use a sound recorder and I want to plug the recorder and grab the most recent sound.

That's what this command does.

Use mv instead of cp to move instead of copy.

Change *.wav to the required file type.

cp -r path/to/file/tree $(mkdir -p new/path/here; echo new/path/here)
2012-04-27 16:18:11
User: wirespeed
Functions: cp echo mkdir

You need to cp, mv, scp, ..., some files around from one place to another, and after having laboriously typed out the source path, you remember that the destination directory doesn't yet exist, and so the command will fail. So rather than killing the command line and starting over, just interpolate the results of creating the directory and echo its name. You could DRY this with a for; do; done, but that may be more trouble than it's worth.

find / -type f -name IMG_????.JPG -print0 |xargs -0 exiv2 -g Exif.Canon.ModelID '{}' |grep A520 |rev |cut --complement -d " " -f1-40 |rev |xargs -I {} cp --parents {} /where
2012-03-10 03:01:01
User: fladam
Functions: cp cut find grep rev xargs

You must spezify /where folder and / folder

If you have another camera you must experiment with Exif data (after -g and after grep) and mask of your photo files IMG_????.JPG

I have do it on Knoppix 6.7.0

You must have installed exiv2.