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 tagged copy from sorted by
Terminal - Commands tagged copy - 33 results
rsync -arvz -e 'ssh -p 2233' --progress --delete remote-user@remote-server.org:/path/to/folder /path/to/local/folder
2014-09-26 10:42:26
User: nadavkav
Functions: rsync
0

Useful, when you need to backup/copy/sync a folder over ssh with a non standard port number

find * -regextype posix-extended -regex '.*\.(ext_1|ext_2)' -exec cp {} copy_target_directory \;
rsync -v -r --size-only --compare-dest=../A/ B/ C/
2013-09-10 21:41:16
User: knoppix5
Functions: rsync
8

Assumed dir A, B, C are subdirs of the current dir

Exact syntax of the command is:

rsync -v -r --size-only --compare-dest=/path_to_A/A/ /path_to_B/B/ /path_to_C/C/

(do not omit end-slashes, since that would copy only the names and not the contents of subdirs of dir B to dir C)

You can replace --size-only with --checksum for more thorough file differences validation

Useful switch:

-n, --dry-run perform a trial run with no changes made

rsync -a --append source-file destination
ssh-copy-id [-i [identity_file]] [user@]machine
cat ~/.ssh/id_rsa.pub | ssh user@host 'cat >> ~/.ssh/authorized_keys'
pwd | xclip
2012-06-12 20:03:47
User: ala2200
Functions: pwd
1

Copy the current path. Use -selection clipboard to copy the string to clipboard.

rsync -avz ~/src ~/des/
2012-06-01 15:08:12
User: axelabs
Functions: rsync
0

rsync will copy the source directory into destination and any subsequent run will synchronize only the changes from the source.

rsync -aP --no-whole-file --inplace
2012-01-29 18:39:31
User: jlaunay
Functions: rsync
0

a : to keep files permissions

--no-whole file : use rsync?s delta-transfer algorithm

--inplace : writes the updated data directly to the destination file

optionnal -> add --remove-source-files to mv instead of cp

dpkg-repack firefox
2012-01-13 11:47:40
User: knoppix5
8

If any changes have been made to the package while it was unpacked (ie, conffiles files in /etc modi‐fied), the new package will inherit the changes.

This way you can make it easy to copy packages from one computer to another, or to recreate packages that are installed on your system, but no longer available elsewhere.

Note: dpkg-repack will place the created package in the current directory.

rsync --daemon --port 9999 --no-detach -v --config .rsyncd.conf
2011-09-22 20:48:31
User: pykler
Functions: rsync
-3

An example config file is placed in the sample output along with the command line call to use it.

The rsync daemon here is setup on the destination, thus requiring the read only = false flag. Also it uses uid and gid of root, change as required.

rsync -rv <src> <dst> --progress
2011-08-05 09:29:12
User: fecub
Functions: rsync
10

-r for recursive (if you want to copy entire directories)

src for the source file (or wildcards)

dst for the destination

--progress to show a progress bar

find . -iname "*.flac" | cpio -pdm /Volumes/Music/FLAC
find . -type d -exec mkdir /new/path/{} \;
2011-07-18 05:17:39
User: paulochf
Functions: find mkdir
-1

It's not better than the former, just another possible way.

Found at http://www.linuxquestions.org/questions/linux-newbie-8/copy-directory-structure-only-208796/

Credits to whansard

The command finds all .mp3 files in all subfolders from where it's ran, catches its "relative path" and creates inside /new/path/ with the same "relative path".

PS: /new/path/ must exists

Use case: folder with flac files with tree structure ../artist/album/number-title.flac

1) convert flac->mp3 in the same folder: http://www.commandlinefu.com/commands/view/6341/convert-all-.flac-from-a-folder-subtree-in-192kb-mp3

2) search for mp3 files and recreate tree structure to another path: this command

3) move all mp3 files to that new folder: http://www.commandlinefu.com/commands/view/8854/move-mp3-files-to-another-path-with-existing-subtree-structure

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.

cd $srcdir && find -type d -exec mkdir -p $dstdir/{} \;
2010-12-18 12:13:39
User: bashrc
Functions: cd find mkdir
Tags: copy
1

Clone directory structure without the files

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.

dd if=/path/inputfile | pv | dd of=/path/outpufile
dd if=/path/to/inputfile of=/path/to/outputfile & pid=$! && sleep X && while kill -USR1 $pid; do sleep X; done
2010-12-02 15:07:18
User: cyrusza
Functions: dd kill sleep
Tags: dd copy progress
1

Adjust "sleep X" to your needs.

*NOTE: First sleep is required because bash doesn't have a "post-test" syntax (do XXX while).

dir='path to file'; tar cpf - "$dir" | pv -s $(du -sb "$dir" | awk '{print $1}') | tar xpf - -C /other/path
2010-01-19 19:05:45
User: starchox
Functions: awk dir du tar
Tags: copy tar cp
-2

This may seem like a long command, but it is great for making sure all file permissions are kept in tact. What it is doing is streaming the files in a sub-shell and then untarring them in the target directory. Please note that the -z command should not be used for local files and no perfomance increase will be visible as overhead processing (CPU) will be evident, and will slow down the copy.

You also may keep simple with, but you don't have the progress info:

cp -rpf /some/directory /other/path
dc3dd progress=on bs=512 count=2048 if=/dev/zero of=/dev/null
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

ssh <host> 'tar -cz /<folder>/<subfolder>' | tar -xvz
2009-11-10 20:06:47
User: polaco
Functions: ssh tar
9

This command will copy a folder tree (keeping the parent folders) through ssh. It will:

- compress the data

- stream the compressed data through ssh

- decompress the data on the local folder

This command will take no additional space on the host machine (no need to create compressed tar files, transfer it and then delete it on the host).

There is some situations (like mirroring a remote machine) where you simply cant wait for a huge time taking scp command or cant compress the data to a tarball on the host because of file system space limitation, so this command can do the job quite well.

This command performs very well mainly when a lot of data is involved in the process. If you copying a low amount of data, use scp instead (easier to type)