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 touch from sorted by
Terminal - Commands using touch - 47 results
touch -r "source_file" "destination_file"
tstouch() { [[ $1 =~ $2 ]] && touch -t ${BASH_REMATCH[1]} $1; }
2013-10-01 20:00:34
User: bartonski
Functions: touch
Tags: bash touch
2

tstouch takes two arguments: a filename containing a timestamp, and an extended regular expression with the parenthesized section matching a timestamp of the form YYYYMMDDhhmm or YYYYMMDDhhmm.ss.

It then touches the file with that timestamp.

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)

touch file{1..10}.txt ; ls *txt| sed -e "p;s/\.txt$/\.csv/"|xargs -n2 mv
touch ~/.hushlogin
2012-12-05 18:03:41
User: elofland
Functions: touch
Tags: login motd
6

I'm annoyed by the boilerplate "don't login unless you are supposed messages in our environment" - this shuts them up.

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.

touch filename
touch -t 201208211200 first ; touch -t 201208220100 last ; find /path/to/files/ -newer first ! -newer last | xargs -ifile mv -fv file /path/to/destination/ ; rm first; rm last;
2012-08-22 09:51:40
User: ktopaz
Functions: file find last mv rm touch xargs
0

touch -t 201208211200 first ; touch -t 201208220100 last ;

creates 2 files: first & last, with timestamps that the find command should look between:

201208211200 = 2012-08-21 12:00

201208220100 = 2012-08-22 01:00

then we run find command with "-newer" switch, that finds by comparing timestamp against a reference file:

find /path/to/files/ -newer first ! -newer last

meaning: find any files in /path/to/files that are newer than file "first" and not newer than file "last"

pipe the output of this find command through xargs to a move command:

| xargs -ifile mv -fv file /path/to/destination/

and finally, remove the reference files we created for this operation:

rm first; rm last;

cd / && touch ./\-i
2012-04-05 20:55:37
User: joedhon
Functions: cd touch
-10

Somehow, i prefer forcing to rm interactively to accidently rm'ing everything...

find . -newer /tmp/foo -exec touch --date "2011-12-12" {} \;
2011-12-15 04:55:57
User: djangofan
Functions: find touch
0

Modify all files newer than another file and touch them to a specific date.

touch -d $(zenity --calendar --date-format=%F) filename
function right { bc <<< "obase=8;ibase=2;$1"; }; touch foo; chmod $(right 111111011) foo; ls -l foo
2011-11-16 22:43:31
User: nerd
Functions: bc chmod ls touch
0

I simply find binary notation more straightforward to use than octal in this case.

Obviously it is overkill if you just 600 or 700 all of your files...

touch --date "2010-01-05" /tmp/filename
for i in /usr/bin/* ;do touch ${i##*/}; done
2011-10-20 12:38:45
User: _john
Functions: touch
Tags: bash find xargs zsh
0

You could avoid xargs and sed in this case (shorter command and less forking): At least bash and zsh have some mighty string modifiers.

I would also suggest using find with exec option to get more flexibility. You may leave out or include "special" file for example.

touch -t [[CC]AA]MMJJhhmm[.ss]
for i in `seq 100`; do mkdir f${i}; touch ./f${i}/myfile$i ;done
2011-09-29 01:03:46
Functions: mkdir touch
Tags: seq mkdir touch
0

creates 100 directories f(1-100) with a file in each matched to the directory (/f1/myfile1, .. /f98/myfile98,/f99/myfile99/,/f100/myfile100,etc )

switchMonitor () { LF=/tmp/screen-lock; if [ -f $LF ]; then rm $LF; else touch $LF; sleep .5; while [ -f $LF ]; do xset dpms force off; sleep 2; done; fi };
2011-08-26 17:55:44
User: totti
Functions: rm sleep touch
1

Use the command to create a script and bind it to a key using keyboard shortcut.

eg:

Script locks the screen in a loop until the command is executed again.At first it

find . -type f -exec touch "{}" \;
touch -t 201001010000 begin; touch -t 201012312359.59 end; find . -newer begin -a ! -newer end
2011-06-22 20:09:05
Functions: find touch
Tags: find dates touch
1

Example above will recursively find files in current directory created/modified in 2010.

touch file{1,2,3,4,5}.sh
cd <directory>; touch ./-i
2011-05-12 11:01:58
User: ljmhk
Functions: cd touch
Tags: touch
15

Forces the -i flag on the rm command when using a wildcard delete.

touch pk.pem && chmod 600 pk.pem && openssl genrsa -out pk.pem 2048 && openssl req -new -batch -key pk.pem | openssl x509 -req -days 365 -signkey pk.pem -out cert.pem
2011-05-11 18:09:33
User: bfreis
Functions: chmod touch
1

This will create, in the current directory, a file called 'pk.pem' containing an unencrypted 2048-bit RSA private key and a file called 'cert.pem' containing a certificate signed by 'pk.pem'. The private key file will have mode 600.

!!ATTENTION!! ==> this command will overwrite both files if present.

find . -exec touch {} \;
touch file
touch /path/to/file.txt