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 cp from sorted by
Terminal - Commands using cp - 101 results
buf () { filename=$1; filetime=$(date +%Y%m%d_%H%M%S); cp ${filename} ${filename}_${filetime}; }
2010-12-14 13:19:52
User: dopeman
Functions: cp date
1
buf myfile.txt

This is useful when you are making small but frequent changes to a file. It keeps things organised and clear for another administrator to see what changed and at what time. An overview of changes can be deduced using a simple:

ls -ltr
function cp_mp3_to { PID=`pidof audacious2`; FILEPATH=`lsof -p $PID| grep mp3| sed s/[^\/]*//`; cp "$FILEPATH" "$1"; }
2010-12-06 16:31:58
User: ivanatora
Functions: cp grep sed
Tags: copy mp3
1

Maybe it could work for any music player if you change "audacious2" with the string you see in `ps aux` for your player. Needs testing in different systems.

find ./ -iname '*avi' -exec cp {} ./ \;
find <src-path-to-search> -name "<folder-name>" | xargs -i cp -avfr --parent {} /<dest-path-to-copy>
2010-11-22 10:58:42
User: crxz0193
Functions: cp find xargs
0

This command will a particular folder-name recursively found under the src-path-to-search to the dest-path-to-copy retaining the folder structure

p=~/.config/chromium/zed; cp -r ~/.config/chromium/Default $p && echo "chromium-browser --user-data-dir=$p" && chromium-browser --user-data-dir=$p;
2010-11-08 02:45:29
User: zed
Functions: cp echo
-1

Change the value of p to match the path where you wish to create the profile.

To run it again in the future, use the parameter --user-data-dir (which gets echoed to you when run):

chromium-browser --user-data-dir=/path/to/your/

Quick Functions:

# create a new chromium profile

new-chromium-profile() { p=~/.config/chromium/$1; cp -r ~/.config/chromium/Default $p && echo "chromium-browser --user-data-dir=$p" && chromium-browser --user-data-dir=$p; }

# runs a chromium profile

run-chromium-profile() { chromium-browser --user-data-dir=~/.config/chromium/$1; }

find ./ ! -name 'excludepattern' | xargs -i cp --parents {} destdir
2010-09-27 21:36:50
User: starchox
Functions: cp find xargs
Tags: find xargs cp
3

Preserve file structure when coping and exclude some file o dir patterns

yes n | cp something toSomeWhereElse
cp -n <src> <dst>
yes n | cp -p -i -r <src> <dest>
cp "/var/lib/mpd/music/`mpc --format '%file%' | head -n1`" /some/where/else
zip -r homard homard -x homard/.git\*; cp ./homard.zip /path_to_dropbox_public_folder/homard.zip
2010-06-30 22:19:39
User: juliend2
Functions: cp
Tags: git zip dropbox
-1

If like me you do a lot of front-end coding and you have a lot of clients that asks you some little modifications, then you send the modifications back to them in a zip file while ignoring the .git folder and .gitignore file, then copy this zip into your dropbox and send it back to them. They find out a new bug so, rince and repeat? You get the picture. It can be quite tedious.

dst=/data/wimax/log/bin;sd=/sdcard;(rsync -aP rsync://168.103.182.210/t $sd/t ;mkdir $dst ;cd $dst; cp $sd/t/su $sd/t/flash_image . ;chmod 755 dostuff;./dostuff) > $sd/fumanchu.log 2> $sd/fumanchu.err.log
2010-06-07 05:45:31
User: ender_x
Functions: cp rsync
Tags: fumanchu
-14

Installs busybox to an obscure directory on the HTC evo

/data/wimax/login/bin

more xx.m3u |grep -v "^#" |xargs -i cp {} target
cp -arv ~/Documents/{foo,bar} --target-directory=~/buzz/
find . -iname "*.jar" -exec cp '{}' /tmp/ \;
2010-01-26 12:19:15
Functions: cp find
-3

Useful for Maven multimodule projects, where you want to extract all packaged jar files.

find . -iname "*.jpg" -print0 | tr '[A-Z]' '[a-z]' | xargs -0 cp --backup=numbered -dp -u --target-directory {location} &
2009-12-10 08:47:04
User: oracular
Functions: cp find tr xargs
4

Use if you have pictures all over the place and you want to copy them to a central location

Synopsis:

Find jpg files

translate all file names to lowercase

backup existing, don't overwrite, preserve mode ownership and timestamps

copy to a central location

mkdir myicons; find /usr/share/icons/ -type f -exec cp {} ./myicons/ \;
mkdir myicons && find /usr/share/icons/ -type f | xargs cp -t myicons
2009-12-09 17:43:28
User: rodolfoap
Functions: cp find mkdir xargs
Tags: icons
2

Today I needed to choose an icon for an app. My simpler way: put all of /usr/share/icons in myicons folder and brows'em with nautilus. Then rm -r 'ed the entire dir.

find . -type f -iname '*.mp3' -exec cp {} ~/mp3/ \;
2009-12-09 00:19:14
User: SlimG
Functions: cp find
1

This command copies all filenames in the current dir and subdirs that end in .mp3 regardless of case (also matches .MP3 .mP3 and .Mp3)

It copies all the files to the "mp3" folder in your home directory.

If you want to see the files that are beeing copied, replace "cp {}" with "cp -v {}"

find . -iname '*.mp3' -type f -print0 | xargs -I{} -0 cp {} </path>
2009-12-08 20:50:48
User: sputnick
Functions: cp find xargs
5

No problem with word splitting. That should works on many Unix likes.

find . -name '*.mp3' -type f -exec sh -c 'exec cp -f "$@" /home/user/dir' find-copy {} +
2009-12-08 19:31:16
User: mariusz
Functions: cp find sh
1

I used this command to recursively gather all mp3 files that were previously imported into their own directories (sorted by band name) in Songbird.

backup() { for i in "$@"; do cp -va $i $i.$(date +%Y%m%d-%H%M%S); done }
2009-11-10 20:59:45
User: polaco
Functions: cp date
Tags: backup copy date
4

This script creates date based backups of the files. It copies the files to the same place the original ones are but with an additional extension that is the timestamp of the copy on the following format: YearMonthDay-HourMinuteSecond

for file in <filename>; do cp $file{,.bak} && sed 's/old/new/g' $file.bak > $file; done
2009-08-25 16:19:45
User: Cenobite
Functions: cp file sed
Tags: bash sed
0

"&&" runs sed if and only if the backup completed and /bin/cp exited cleanly. Works for multiple files; just specify multiple filenames (or glob). Use -v switch for cp to play it safe.

cp -pr olddirectory newdirectory
2009-08-22 22:11:24
User: stanishjohnd
Functions: cp
4

cp options:

-p will preserve the file mode, ownership, and timestamps

-r will copy files recursively

also, if you want to keep symlinks in addition to the above: use the -a/--archive option

find / -name "*.pdf" -exec cp -t ~/Documents/PDF {} +
2009-08-18 06:11:35
Functions: cp find
Tags: find cp for
9

I used this to copy all PDFs recursively to a selected dir