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
lsof -n -P|grep FlashXX|awk '{ print "/proc/" $2 "/fd/" substr($4, 1, length($4)-1) }'|while read f;do newname=$(exiftool -FileModifyDate -FileType -t -d %Y%m%d%H%M%S $f|cut -f2|tr '\n' '.'|sed 's/\.$//');echo "$f -> $newname";cp $f ~/Vids/$newname;done
2012-02-25 01:49:45
User: mhs
Functions: awk cp cut echo grep read sed tr
8

Certain Flash video players (e.g. Youtube) write their video streams to disk in /tmp/ , but the files are unlinked. i.e. the player creates the file and then immediately deletes the filename (unlinking files in this way makes it hard to find them, and/or ensures their cleanup if the browser or plugin should crash etc.) But as long as the flash plugin's process runs, a file descriptor remains in its /proc/ hierarchy, from which we (and the player) still have access to the file. The method above worked nicely for me when I had 50 tabs open with Youtube videos and didn't want to have to re-download them all with some tool.

ldd file | grep "=> /" | awk '{print $3}' | xargs -I '{}' cp -v '{}' /destination
2012-02-11 17:50:10
User: rickard2
Functions: awk cp file grep ldd xargs
0

When working with jailed environments you need to copy all the shared libraries to your jail environment. This is done by running ldd on a binary which needs to run inside the jail. This command will use the output from ldd to automatically copy the shared libraries to a folder of your choice.

for FILE in `ls -1`; do if [ -L "$FILE" ]; then cp $(readlink "$FILE") ${FILE}_rf; rm -f $FILE; mv ${FILE}_rf "$FILE"; fi; done
cp -r * .??* /dest
2011-12-16 23:41:03
User: atoponce
Functions: cp
Tags: mv rm cp
6

You could do the following, however, brace expansion with {} is not defined in POSIX, and therefore not guaranteed to work in all shells. But, if it does, it's more convenient (although it's certainly not less typing):

cp -r {*,.??*} /dest

Sometimes there are times when I need to cp(1), mv(1) or rm(1) files recursively, but don't want to traverse the previous directory by following ../../../../ etc out of the current directory. This command prevents that. The secret sauce is ".??*". The file globbing ensures that it must start with a dot, and be followed by at least two characters. So, three characters must exist in the filename, which eliminates "." and "..".

cp -a /etc /destination
find <start directory> -iname "<all my files type>" -exec cp {} <target_dir> \;
cp /work/host/phone/ui/main.cpp !#$:s/host/target
2011-09-20 06:48:07
User: kev
Functions: cp
Tags: !!
33

expand to:

cp /work/host/phone/ui/main.cpp /work/target/phone/ui/main.cpp

!#

The entire command line typed so far.

cp --sparse=always <SRC> <DST>
2011-09-07 08:02:50
User: h3xx
Functions: cp
4

This causes cp to detect and omit large blocks of nulls. Sparse files are useful for implying a lot of disk space without actually having to write it all out.

http://en.wikipedia.org/wiki/Sparse_file

You can use it in a pipe too:

dd if=/dev/zero bs=1M count=5 |cp --sparse=always /dev/stdin SPARSE_FILE
cp -dpRx /* /mnt/target/
2011-08-22 14:22:01
Functions: cp
-1

preserve all except context and xattr. useful when moving a running system to a new partition.

sedit() { cp "$*"{,.bk}; which $EDITOR > /dev/null && $EDITOR "$*" || vim "$*"; }
2011-08-16 18:28:22
User: kaedenn
Functions: cp vim which
-6

Some people put spaces in filenames. Others have an $EDITOR environment variable set. This defaults to vim, but you can use whatever you wish: emacs, nano, ed, butterflies, etc.

cp httpd.conf{,.bk}
2011-08-15 16:43:53
User: ideivid
Functions: cp
Tags: backup cp
17

Remember to backup everything before changing it so you can restore all to normal.

cp /dev/cdrom file.iso
find ~ -name "*.ttf" -exec cp {} /usr/share/fonts/truetype \; & fc-cache -f
2011-05-25 20:18:33
User: scripteles
Functions: cp fc-cache find
0

1) Find true type fonts;

2) Copy them to /usr/share/fonts/truetype;

3) Reload font information.

for file in `find *| sort -n | sed 's% %?%g'`; do echo "${file//?/ }"; cp --parents "${file//?/ }" /destinity_folder/ ;done
v () { ( IFS=$'\n'; suf="_versions"; mkdir -p "$1$suf"; nr=`ls "$1$suf" | wc -l`; nr=`printf "%02d" $(($nr + 1))`; cp "$1" "$1$suf/v${nr}_$1" ) }
2011-04-22 07:33:51
User: dubnov
Functions: cp mkdir wc
Tags: bash copy
0

Bash function copies a file prefixed with a version number to a subdirectory

cp filename{,.`date +%Y%m%d`}
2011-04-02 06:41:26
Functions: cp
Tags: backup copy
4

This is a BASH feature.

The above command will create a backup of "filename" called "filename.DATE", where DATE is the actual day in %Y%m%d (year, month and day numbers together) format.

files -type f | xargs -n100 | while read l; do mkdir $((++f)); cp $l $f; done
2011-02-15 23:15:16
User: flatcap
Functions: cp mkdir read xargs
-2

Take a folder full of files and split it into smaller folders containing a maximum number of files. In this case, 100 files per directory.

find creates the list of files

xargs breaks up the list into groups of 100

for each group, create a directory and copy in the files

Note: This command won't work if there is whitespace in the filenames (but then again, neither do the alternative commands :-)

find files/ -type f | while read line; do if [ $((i++%100)) -eq 0 ]; then mkdir $((++folder)); fi; cp $line $folder/; done
s=/etc/ssh/sshd_config;r=PermitRootLogin;cp $s{,.old}&& if grep $r $s;then sed "s/$r yes/$r no/" $s.old > $s; else echo $r no >> $s;fi
2011-01-30 23:41:59
User: kzh
Functions: cp echo grep sed
Tags: bash openssh
-1

This will tighten up security for your box. The default value for PermitRootLogin sadly is 'yes'.

cp -i FILENAME{,.`date +%Y%m%d`}
buf() { cp -v $1 ${1/${1%%.*}/$f-$(date +"%Y%m%d_%H%M%S")};}
buf() { f=${1%%.*};e=${1/$f/};cp -v $1 $f-$(date +"%Y%m%d_%H%M%S")$e;}
2010-12-15 09:50:04
User: unefunge
Functions: cp date
0

"infix" version in bash (4.x+)

Remove -v to make it silent.

BTW: The OP forgot to use "cat" and "nmap" ;-) I had a good laugh though.

buf () {oldname=$1; if [ "$oldname" != "" ]; then datepart=$(date +%Y-%m-%d); firstpart=`echo $oldname | cut -d "." -f 1`; newname=`echo $oldname | sed s/$firstpart/$firstpart.$datepart/`; cp -i ${oldname} ${newname}; fi }
2010-12-14 19:58:34
User: Seebi
Functions: cp cut date sed
-3

This backup function preserve the file suffix allowing zsh suffix aliases and desktop default actions to work with the backup file too.

buf () { cp $1{,$(date +%Y%m%d_%H%M%S)}; }
2010-12-14 14:02:03
User: unefunge
Functions: cp date
2

1. you don't need to prepend the year with 20 - just use Y instead of y

2. you may want to make your function a bit more secure:

buf () { cp ${1?filename not specified}{,$(date +%Y%m%d_%H%M%S)}; }