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 mv from sorted by
Terminal - Commands tagged mv - 26 results
imv foo
2014-01-24 01:34:01
User: denilsonsa
Tags: mv cp
0

icp and imv, interactive versions of cp and mv, will prompt the user for the new filename. These tools are from rename-utils package.

for file in $(git ls-files | grep old_name_pattern); do git mv $file $(echo $file | sed -e 's/old_name_pattern/new_name_pattern/'); done
find ./ -type f -exec sh -c 'echo "{}" "$(dirname "{}")/$(basename "{}" | tr "[A-Z]" "[a-z]")"' \;
2012-06-14 07:13:42
User: jelloir
Functions: find sh
Tags: bash find mv rename tr
0

Handles spaces in file names and directories. Optionally change directories as well by pipe to tr from dirname.

cp -r * .??* /dest
2011-12-16 23:41:03
User: atoponce
Functions: cp
Tags: mv rm cp
5

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 "..".

rename s/^/./ *
for file in *.mp4; do mv "$file" "${file%.*} [$(cksfv -b -q "$file" | egrep -o "\b[A-F0-9]{8}\b$")].${file#*.}"; done
mv -b old_file_name new_and_already_existent_file_name
2011-09-08 23:57:15
User: ztank1013
Functions: mv
Tags: backup mv cp safe
5

Sometimes in a hurry you may move or copy a file using an already existent file name. If you aliased the cp and mv command with the -i option you are prompted for a confirmation before overwriting but if your aliases aren't there you will loose the target file!

The -b option will force the mv command to check if the destination file already exists and if it is already there a backup copy with an ending ~ is created.

IFS=?" ; for i in * ; do mv -v $i `echo $i|tr ???????????????????\ aaaeeiooAAAEEIOOOcC_` ; done
rf() { for i in "$@"; do mv "$i" "$(pwgen 8 1).${i##*.}"; done }
2011-06-22 07:45:23
User: flatcap
Functions: mv
1

Give files a random name (don't ask why :-)

The function will rename files but maintain their extensions.

BUG: If a file doesn't have an extension it will end up with a dot at the end of the name.

The parameter '8' for pwgen controls the length of filenames - eight random characters.

ls *.jpg | awk -F'.' '{ printf "%s %04d.%s\n", $0, $1, $2; }' | xargs -n2 mv
2011-05-01 13:32:58
User: hute37
Functions: awk ls printf xargs
Tags: awk mv xargs
3

rename file name with fixed length nomeric format pattern

find . -depth -print -execdir rename -f 'y/A-Z/a-z/' '{}' \;
2011-03-25 03:10:27
User: rsimpson
Functions: find rename
Tags: bash find mv rename tr
1

easier way to recursively change files to lowercase using rename instead

flipf(){ if [ -f "$1" -a -f "$2" ]; then mv "$1" "$1.$$" && mv "$2" "$1" && mv "$1.$$" "$2" || echo "$!"; else echo "Missing a file: $!"; fi; }
mv -n * ../; cd ..; rmdir $OLDPWD
2010-11-12 20:22:50
User: kevingranade
Functions: cd mv rmdir
Tags: mv rmdir
0

Avoid clobbering files by either overwriting due to name collisions or by assuming the command worked and deleting the target directory.

for i in *; do mv $i prependtext$i; done
ls | while read -r FILE; do mv -v "$FILE" `echo "prependtext$FILE" `; done
2010-08-14 14:19:18
User: IgnitionWeb
Functions: ls mv read
Tags: echo mv prepen
-4

Prepends all directory items with "prependtext"

find . -type d -name '*[A-Z]*' -execdir bash -c '! test -f "$(echo "$0" | tr "[:upper:]" "[:lower:]")"' {} \; -execdir bash -c 'mv "$0" "$(echo "$0" | tr "[:upper:]" "[:lower:]")"' {} \;
find -type f -exec mv {} . \;
2010-03-02 07:09:45
User: and3k
Functions: find mv
10

Find every file and move it to current directory.

ls -d */* | sed -e 's/^/\"/g' -e 's/$/\"/g' | xargs mv -t $(pwd)
2010-03-01 23:43:26
User: leovailati
Functions: ls mv sed xargs
-1

You WILL have problems if the files have the same name.

Use cases: consolidate music library and unify photos (especially if your camera separates images by dates).

After running the command and verifying if there was no name issues, you can use

ls -d */ | sed -e 's/^/\"/g' -e 's/$/\"/g' | xargs rm -r

to remove now empty subdirectories.

for i in */*/*\(1\)*; do mv -f "$i" "${i/ (1)}"; done
2010-01-30 03:11:55
User: magenine
Functions: mv
1

Renames duplicates from MusicBrainz Picard, so you get the latest copy and not a bunch of duplicates.

find /path/to/images -name '*.JPG' -exec bash -c 'mv "$1" "${1/%.JPG/.jpg}"' -- {} \;
2010-01-07 15:41:17
User: sorpigal
Functions: bash find
Tags: bash find mv
7

Recursively rename .JPG to .jpg using standard find and mv. It's generally better to use a standard tool if doing so is not much more difficult.

for f in $(ls *.xml.skippy); do mv $f `echo $f | sed 's|.skippy||'`; done
2009-11-19 21:36:26
User: argherna
Functions: ls mv sed
Tags: sed ls mv for
-2

For this example, all files in the current directory that end in '.xml.skippy' will have the '.skippy' removed from their names.

find . -name "-help" -exec mv {} help.txt \;
for i in *;do mv "$i" "$(echo $i | sed s/PROBLEM/FIX/g)";done
2009-06-28 01:50:25
User: AlecSchueler
Functions: mv
3

Useful if non-ascii characters in filenames have been improperly encoded. Replace "PROBLEM" with the incorrect characters (e.g. 'é'), and "FIX" with the correct ones (e.g. '?').

ls | sed -n -r 's/banana_(.*)_([0-9]*).asc/mv & banana_\2_\1.asc/gp' | sh
2009-04-28 17:53:58
User: log0
Functions: ls sed
Tags: sed mv rename
6

A powerfull way to rename file using sed groups.

& stand for the matched expression.

\1 referes to the first group between parenthesis. \2 to the second.

find -depth . | (while read FULLPATH; do BASENAME=`basename "${FULLPATH}"`; DIRNAME=`dirname "${FULLPATH}"`; mv "${DIRNAME}/${BASENAME}" "${DIRNAME}/${BASENAME// /_}"; done)
2009-03-24 21:04:32
User: mohan43u
Functions: find mv read
-9

Takes filenames and directory names and replace space to '_'.