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 rename from sorted by
Terminal - Commands tagged rename - 35 results
rename 's/\.sh//' ./*
2014-04-02 16:33:25
User: abhikeny
Functions: rename
0

The 'rename' command with the first argument as "'s/\.//'" and the second argument as "" will remove the specified extension from the filenames.

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
for i in `find -name '*_test.rb'` ; do mv $i ${i%%_test.rb}_spec.rb ; done
2012-10-09 14:08:38
User: olopopo
Functions: mv
0

Renames all files ending in "_test.rb" to "_spec.rb"

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.

for i in *.jpg; do dst=$(exif -t 0x9003 -m $i ) && dst_esc=$(echo $dst | sed 's/ /-/g' ) && echo mv $i $dst_esc.jpg ; done
2012-05-02 07:23:38
User: klisanor
Functions: echo mv sed
Tags: exif date rename
0

The command renames all files in a certain directory. Renaming them to their date of creation using EXIF. If you're working with JPG that contains EXIF data (ie. from digital camera), then you can use following to get the creation date instead of stat.

* Since not every file has exif data, we want to check that dst is valid before doing the rest of commands.

* The output from exif has a space, which is a PITA for filenames. Use sed to replace with '-'.

* Note that I use 'echo' before the mv to test out my scripts. When you're confident that it's doing the right thing, then you can remove the 'echo'... you don't want to end up like the guy that got all the files blown away.

Credits: http://stackoverflow.com/questions/4710753/rename-files-according-to-date-created

find . -depth -name '* *' -execdir bash \-c 'a="{}";mv -f "$a" ${a// /_}' \;
2012-02-28 04:03:40
User: DewiMorgan
Functions: bash find mv
0

Sometimes, you don't want to just replace the spaces in the current folder, but through the whole folder tree - such as your whole music collection, perhaps. Or maybe you want to do some other renaming operation throughout a tree - this command's useful for that, too.

To rename stuff through a whole directory tree, you might expect this to work:

for a in `find . -name '* *'`;do mv -i "$a" ${a// /_};done

No such luck. The "for" command will split its parameters on spaces unless the spaces are escaped, so given a file "foo bar", the above would not try to move the file "foo bar" to "foo_bar" but rather the file "foo" to "foo", and the file "bar" to "bar". Instead, find's -execdir and -depth arguments need to be used, to set a variable to the filename, and rename files within the directory before we rename the directory.

It has to be -execdir and won't work with just -exec - that would try to rename "foo bar/baz quux" to "foo_bar/baz_quux" in one step, rather than going into "foo bar/", changing "baz quux" to "baz_quux", then stepping out and changing "foo bar/" into "foo_bar/".

To rename just files, or just directories, you can put "-type f" or "-type d" after the "-depth" param.

You could probably safely replace the "mv" part of the line with a "rename" command, like rename 'y/ /_/' *, but I haven't tried, since that's way less portable.

for i in *ext; do mv $i ${i%.ext}; done
2011-11-13 03:58:08
User: paulochf
Functions: mv
-3

For those files in current folder that would be shown in `ls *ext`, for some extension ext, move/rename that file removing the .ext suffix from the file name.

It uses Bash's parameter substitution, as seen in

http://tldp.org/LDP/abs/html/parameter-substitution.html#PCTPATREF

(for analog use in prefix, see http://tldp.org/LDP/abs/html/parameter-substitution.html#PSOREX2 )

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.

mv file.png $( mktemp -u | cut -d'.' -f2 ).png
rename 's/\d+/sprintf("%04d",$&)/e' *.jpg
2011-05-01 20:50:36
User: eightmillion
Functions: rename
11

This uses Perl's rename utility (you may have to call it as prename on your box) and won't choke on spaces or other characters in filenames. It will also zero pad a number even in filenames like "vacation-4.jpg".

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

exec 3<&0; ls -1N | while read a; do echo "Rename file: $a"; read -e -i "$a" -p "To: " b <&3 ; [ "$a" == "$b" ] || mv -vi "$a" "$b"; done
perl -wlne'/title>([^<]+)/i&&rename$ARGV,"$1.html"' *.html
2010-12-29 05:39:41
User: mhs
Functions: perl
Tags: rename
9

The above one-liner could be run against all HTML files in a directory. It renames the HTML files based on the text contained in their title tag. This helped me in a situation where I had a directory containing thousands of HTML documents with meaningless filenames.

for i in ???.jpg; do mv $i $(printf %04d $(basename $i .jpg) ).jpg ; done
2010-11-18 23:48:41
User: carlesso
Functions: basename mv printf
Tags: rename cp printf
5

Useful if you have a list of images called 1 2 3 4 and so on, you can adapt it to rewrite it as 4 (in this example) 0-padded number.

rename 's/\.jpe?g$/.jpg/i' *
2010-11-14 21:22:13
User: icebrain
Functions: rename
Tags: rename jpg
14

the "i" controls case sensitiveness. It's slightly inefficient since it uselessly renames .jpg to .jpg, but that's more than compensated by launching only one process instead of two, besides being shorter to write.

rename 's/\.jpeg/\.jpg/' *.jpeg; rename 's/\.JPG/\.jpg/' *.JPG
declare -i i=0 ; for file in * ; do i=$[$i+1] ; mv "$file" $i; done
declare -i i; i=0; for file in *; do i=`expr $i+1`; mv "$file" $i; done;
2010-08-26 12:24:38
User: themiurgo
Functions: file mv
-5

Renames files in a directory to incremental numbers, following alphabetic order. The command does not maintain extensions.

rename 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' *
2010-08-22 15:00:33
User: John_W
Functions: rename
2

All words of the filenames except "a", "of", "that" and "to" are capitalized.

To also match words which begin with a specific string, you can use this:

rename 's/\b((?!hello\b|t)[a-z]+)/\u$1/g' *

This will capitalize all words except "hello" and words beginning with "t".

rename 's/\b([a-z])/\u$1/g' *
rename 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' *
2010-08-22 11:26:20
User: l0b0
Functions: rename
0

Anyone know how to avoid title casing some words, like 'to', 'of', 'that', etc.?

shopt -s extglob; for f in *.ttf *.TTF; do g=$(showttf "$f" 2>/dev/null | grep -A1 "language=0.*FullName" | tail -1 | rev | cut -f1 | rev); g=${g##+( )}; mv -i "$f" "$g".ttf; done
2

Just a quick hack to give reasonable filenames to TrueType and OpenType fonts.

I'd accumulated a big bunch of bizarrely and inconsistently named font files in my ~/.fonts directory. I wanted to copy some, but not all, of them over to my new machine, but I had no idea what many of them were. This script renames .ttf files based on the name embedded inside the font. It will also work for .otf files, but make sure you change the mv part so it gives them the proper extension.

REQUIREMENTS: Bash (for extended pattern globbing), showttf (Debian has it in the fontforge-extras package), GNU grep (for context), and rev (because it's hilarious).

BUGS: Well, like I said, this is a quick hack. It grew piece by piece on the command line. I only needed to do this once and spent hardly any time on it, so it's a bit goofy. For example, I find 'rev | cut -f1 | rev' pleasantly amusing --- it seems so clearly wrong, and yet it works to print the last argument. I think flexibility in expressiveness like this is part of the beauty of Unix shell scripting. One-off tasks can be be written quickly, built-up as a person is "thinking aloud" at the command line. That's why Unix is such a huge boost to productivity: it allows each person to think their own way instead of enforcing some "right way".

On a tangent: One of the things I wish commandlinefu would show is the command line HISTORY of the person as they developed the script. I think it's that conversation between programmer and computer, as the pipeline is built piece-by-piece, that is the more valuable lesson than any canned script.