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 cat from sorted by
Terminal - Commands using cat - 417 results
cat my.ps | nc -q 1 hp4550.mynet.xx 9100
find /var/log/apache2 -name 'access.log*gz' -exec zcat {} \; -or -name 'access.log*' -exec cat {} \;
2010-06-19 08:35:12
User: recursiverse
Functions: cat find zcat
0

This command allows you to stream your log files, including gziped files, into one stream which can be piped to awk or some other command for analysis.

Note: if your version of 'find' supports it, use:

find /var/log/apache2 -name 'access.log*gz' -exec zcat {} + -or -name 'access.log*' -exec cat {} +
cat /proc/acpi/battery/*/state
cat VTS_05_1.VOB VTS_05_2.VOB VTS_05_3.VOB VTS_05_4.VOB > mergedmovie.mpg
cat /proc/acpi/battery/BAT0/state
cat /proc/acpi/ac_adapter/AC0/state
cat /proc/acpi/ac_adapter/ACAD/state
cat > {filename} {your text} [^C | ^D]
mysqldump -q --skip-opt --force --log-error=dbname_error.log -uroot -pmysqlpassword dbname | ssh -C user@sshserver 'cd /path/to/backup/dir; cat > dbname.sql'
2010-05-29 23:06:04
User: esplinter
Functions: cat ssh
9

backup big mysql db to remote machine over ssh. "--skip-opt" option is needed when you can?t allocate full database in ram.

while [ 1 -lt 2 ]; do i=0; COL=$((RANDOM%$(tput cols)));ROW=$((RANDOM%$(tput cols)));while [ $i -lt $COL ]; do tput cup $i $ROW;echo -e "\033[1;34m" $(cat /dev/urandom | head -1 | cut -c1-1) 2>/dev/null ; i=$(expr $i + 1); done; done
2010-05-28 16:07:56
User: dave1010
Functions: cat cut expr head tput
1

Same as original, but works in bash

while :; do integer i=0; COL=$((RANDOM%$(tput cols))); ROW=$((RANDOM%$(tput cols))); while (( i <= COL)) do tput cup $i $ROW; echo "\033[1;34m" $(cat /dev/urandom | head -1 | cut -c1-1) 2>/dev/null; i=$(expr $i + 1); done done
cat > <file_name> << "EOF"
2010-05-27 12:06:30
User: sphere64
Functions: cat
7

1. Issue command

2. After angled bracket appears, enter file contents

3. When done, type "EOF"

cat /etc/*release
2010-05-26 11:58:34
User: dog
Functions: cat
9

Works on nearly all linux distros

cat file | sed -n -r '/^100$|^[0-9]{1,2}$/p'
2010-05-15 19:15:56
User: voyeg3r
Functions: cat file sed
-1

-r to use extended regex

^ begin line

| alternative

get 100 or 0-9 one or two times

cat 1.tar.gz 2.tar.gz | tar zxvif -
2010-05-09 03:50:00
Functions: cat tar
-1

You don't need to create an intermediate file, just pipe the output directly to tar command and use stin as file (put a dash after the f flag).

echo 'Host or User@Host?:'; read newserver && ssh-keygen -N "" -t rsa -f ~/.ssh/id_rsa ; ssh $newserver cat <~/.ssh/id_rsa.pub ">>" ~/.ssh/authorized_keys ; ssh $newserver
2010-05-07 06:24:53
User: alf
Functions: cat echo read ssh ssh-keygen
Tags: ssh ssh-keygen
-2

Some servers don't have ssh-copy-id, this works in those cases.

It will ask for the destination server, this can be IP, hostname, or user@hostname if different from current user.

Ssh keygen will let you know if a pubkey already exists on your system and you can opt to not overwrite it.

cat 1.tar.gz 2.tar.gz > 3.tar.gz; tar zxvfi 3.tar.gz
cat list|while read lines;do echo "USER admin">ftp;echo "PASS $lines">>ftp;echo "QUIT">>ftp;nc 192.168.77.128 21 <ftp>ftp2;echo "trying: $lines";cat ftp2|grep "230">/dev/null;[ "$?" -eq "0" ]&& echo "pass: $lines" && break;done
cat domainlist.txt | while read line; do echo -ne $line; whois $line | grep Expiration ; done | sed 's:Expiration Date::'
2010-05-02 06:49:09
User: netsaint
Functions: cat echo grep read sed whois
3

Create a text file called domainlist.txt with a domain per line, then run the command above. All registries are a little different, so play around with the command. Should produce a list of domains and their expirations date. I am responsible for my companies domains and have a dozen or so myself, so this is a quick check if I overlooked any.

for i in $(cat adm);do echo -e "GET /${i} HTTP/1.0\n\r\n\r \nHost: 192.168.77.128\r\n\r\n \nConnection: close\r\n"|nc -w 1 192.168.77.128 80 |grep -i "200 OK" 2>/dev/null >/dev/null;[ $? -eq "0" ] && echo "Found ${i}" && break;echo "$i";sleep 1;done
cat /sys/devices/virtual/dmi/id/board_name
2010-04-22 03:21:40
User: matthewbauer
Functions: cat
-2

Prints the type of computer you have.

I think this should be used more in distros and other applications because it is so easy to get. This can also be asked by tutorials as an easy way to get your base hardware.

Some alternatives:

sudo dmidecode -s system-product-name

and

sudo smbios-sys-info-lite | sed -n 's/^Product Name: *\(.*\)/\1/p'
LATEST=`readlink /boot/vmlinuz`; OLD=`readlink /boot/vmlinuz.old`; cat /boot/grub/grub.conf | sed -i -e 's/\(Latest \[[^-]*\).*\]/\1-'"${LATEST#*-}"]'/1' -e 's/\(Old \[[^-]*\).*\]/\1-'"${OLD#*-}"]'/1' /boot/grub/grub.conf
2010-04-21 19:16:51
User: algol
Functions: cat sed
1

I like to label my grub boot options with the correct kernel version/build.

After building and installing a new kernel with "make install" I had to edit my grub.conf by hand.

To avoid this, I've decided to write this little command line to:

1. read the version/build part of the filename to which the kernel symlinks point

2. replace the first label lines of grub.conf

grub.conf label lines must be in this format:

Latest [{name}-{version/build}]

Old [{name}-{version/build}]

only the {version/build} part is substituted.

For instance:

title Latest [GNU/Linux-2.6.31-gentoo-r10.201003]

would turn to

title Latest [GNU/Linux-2.6.32-gentoo-r7.201004]"

cat file1 ... fileN > combinedFile;
2010-04-17 01:00:04
User: GinoMan2440
Functions: cat
Tags: cat bash Linux
-4

the last person who posted used the most roundabout way to concatinate files, there's a reason there's a "conCATinate" command... Using this method, you also get to choose the order of the files, below another person just did *.txt > combined.txt which is fine but the order depends on the implementation of "cat" which is probably alphabetical order of filenames.

cat *.txt >output.txt
ls | grep *.txt | while read file; do cat $file >> ./output.txt; done;