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 - 100 results
cp -p file.txt{,.bak}
2014-09-12 13:49:26
User: RedFox
Functions: cp
0

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.

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

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
11

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
-4

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
0

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
0

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
0

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
-2

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
1

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
-1

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
0

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
0

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
-1

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.

cp -auv /SorceDirectory/ /ParentDestination/
2012-03-04 13:04:03
User: joeseggiola
Functions: cp
1

Backup a whole directory copying only updated files.

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.