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
cp path/filename{,-$(date +%Y-%m-%d)}
2009-08-06 13:50:00
User: vutcovici
Functions: cp date
1

It will create a backup of the filename. The advantage is that if you list the folder the backups will be sorted by date. The command works on any unix in bash.

copy(){ cp -v "$1" "$2"&watch -n 1 'du -h "$1" "$2";printf "%s%%\n" $(echo `du -h "$2"|cut -dG -f1`/0.`du -h "$1"|cut -dG -f1`|bc)';}
for f in $(find * -maxdepth 0 -type f); do file=$(find ~/target -name $f); if [ -n "$file" ]; then cp $file ${file}.bak; mv $f $file; fi; done
2009-07-08 10:18:06
User: sanmiguel
Functions: cp find mv
0

You could start this one with

for f in *; do

BUT using the find with "-type f" ensures you only get files not any dirs you might have

It'll also create backups of the files it's overwriting

Of course, this assumes that you don't have any files with duplicated filenames in your target structure

cp texfile.toc texfile.toc.bak; latex texfile.tex; cmp -s texfile.toc texfile.toc.bak; if [ $? -ne 0 ]; then latex texfile.tex; fi
2009-07-07 07:09:45
User: gwiener
Functions: cmp cp
2

To check if the table-of-content in a LaTeX document is up-to-date, copy it to a backup before running LaTeX and compare the new .toc to the backup. If they are identical, it is updated. If not, you need to run LaTeX again.

cp -bfS.bak filename filename
2009-06-30 14:17:28
User: ioggstream
Functions: cp
6

less symbols, tab completion.

including # export SIMPLE_BACKUP_SUFFIX="_`date +%F`" in your .bashrc provides you to easily timestamp your files

cp `ls -x1tr *.jpg | tail -n 1` newest.jpg
2009-06-17 20:32:04
User: Psychodad
Functions: cp tail
1

search the newest *.jpg in the directory an make a copy to newest.jpg. Just change the extension to search other files. This is usefull eg. if your webcam saves all pictures in a folder and you like the put the last one on your homepage. This works even in a directory with 10000 pictures.

find . -name \*.mp3 -printf "%C+ %h/%f\n" | sort -r | head -n20 | awk '{print "\""$2"\""}' | xargs -I {} cp {} ~/tmp
2009-05-17 07:06:10
User: bkinsey
Functions: awk cp find head sort xargs
2

Change ~/tmp to the destination directory, such as your mounted media. Change -n20 to whatever number of files to copy. It should quit when media is full. I use this to put my most recently downloaded podcasts onto my phone.

for i in *jpg; do jpeginfo -c $i | grep -E "WARNING|ERROR" | cut -d " " -f 1 | xargs -I '{}' find /mnt/sourcerep -name {} -type f -print0 | xargs -0 -I '{}' cp -f {} ./ ; done
2009-05-07 00:30:36
User: vincentp
Functions: cp cut find grep xargs
0

Find all corrupted jpeg in the current directory, find a file with the same name in a source directory hierarchy and copy it over the corrupted jpeg file.

Convenient to run on a large bunch of jpeg files copied from an unsure medium.

Needs the jpeginfo tool, found in the jpeginfo package (on debian at least).

for i in {1..5}; do cp test{,$i};done
2009-05-06 21:44:03
User: azeey
Functions: cp
9

Copy a file to a range of other files.

alias backup_dir='mkdir -p .backup && cp * .backup'
2009-04-06 14:43:21
User: k00pa
Functions: alias cp
-3

Add this to .bashrc, then you can quickly create backups from files on current directory, but it only backups files on current directory.

useful when changing config files, coding something or just trying something stupid.

cp --parents /source/file /target-dir
2009-03-23 18:35:15
User: bitmage
Functions: cp
6

The --parents option will cause cp or mkdir to automatically create the parent directory structure.

mkdir --parents /one/two/three/dir

will create /one, /one/two, and /one/two/three as needed before creating dir. cp will copy files with their full directory structure into the target directory with this option.

Thanks to Peter Leung at:

http://linuxcommando.blogspot.com/2007/11/use-of-parents-flag-in-mkdir-and-c.html

which has good examples of usage.

cp ./* .[!.]* ..?* /path/to/dir
2009-03-16 13:27:36
User: ako
Functions: cp
0

./* is for copying files starting with -

.[!.]* is for copying hidden files and avoiding copying files from the parent directory.

..?* is for copying files starting with .. (avoids the directory ..)

/path/to/dir the path to the directory where the files should be copied

Can also be used as a script. Input argument is /path/to/dir

in tcsh, replace .[!.]* with .[^.]*

find . -name "*.pdf" -print0 | xargs -0 cp -t downloads/
2009-03-13 03:15:27
User: abcde
Functions: cp find xargs
1

-t, --target-directory=DIRECTORY (copy all SOURCE arguments into DIRECTORY).

find . -maxdepth 1 -type f -print0 | xargs -0 -i cp ./{}{,.bak}
2009-03-12 16:02:13
User: voyeg3r
Functions: cp find xargs
-2

"." is current dir, maxdepth is the level, -print0 | xargs -0 fix spaces in names, -i interactive , ./ is the current dir {} actual name , and {,.bak} is the atual name + bak

cp --backup=t source.file target.file
2009-03-12 06:46:13
User: magmax
Functions: cp
3

Allows you to preserve your files when using cp, mv, ln, install or patch. When the target file exists, it will generate a file named XXX.~N~ (N is an auto-incremental number) instead of deleting the target file.

find -type -f -exec cp {} {}.bak \;
cp /some/path/to/myfile{,.back}
2009-03-02 04:41:18
User: rockon
Functions: cp
1

Copy the file with the given .extension at the source file's location. Eliminates the typing of long paths again and again.

find . -type f | while read file; do cp $file ${file}.bak; done
2009-03-01 23:42:49
User: atoponce
Functions: cp find read
1

This is a simple case of recursing through all directories, adding the '.bak' extension to every file. Of course, the 'cp $file $file.bak' could be any code you need to apply to your recursion, including tests, other functions, creating variables, doing math, etc. Simple and clean recursion.

alias wordpress='mkdir wordpress && cd wordpress && wget http://wordpress.org/latest.tar.gz && tar -xvzf latest.tar.gz && mv wordpress/* . && rm -rf latest.tar.gz wordpress && cp wp-config-sample.php wp-config.php'
find ~/Music/iTunes/ -name *.mp3 -exec cp {} ~/Desktop/Music/ \;
find start_dir -name *.txt | xargs -J % cp % end_dir/
cp /really/long/path/and/file/name{,-`date -I`}
2009-02-18 20:35:47
User: ozymandias
Functions: cp
4

The expansion {,} in bash will repeat the given string once for each item seperated by commas. The given command will result in the following being run:

cp /really/long/path/and/file/name /really/long/path/and/file/name-`date -I`

These can be embedded as needed, ex: rm file{1,2,3{1,2,3}} would delete the files file1, file2, file31, file32, file32, and no other files.

cp -pr directory-you-want-to-backup{,_`date +%Y%m%d`} # for bash
cp -p file-you-want-backup{,_`date +%Y%m%d`} # for bash
for i in * ; do cp $i $i.bak; done
2009-02-05 15:15:40
User: swinz
Functions: cp
-2

quick in directory backup of all files in this directory. Adds the .bak extension to all copies.