Interesting Linux Commands

Do It With Linux is a blog designed to help the average Linux user, Software Developer, System Admin and anyone who wishes to learn the capabilities of Linux.

On this blog you can expect to find useful Commands, scripts, tips and hacks which can aid you in you workplace or at home.

I've decided to move things about a bit, this post seems to be the most popular so I have decided to move it here and update it on a regular basis.

Use wget to check if a file exists
wget --spider -v
a favourite of mine!
--spider = this makes wget act like a we spider. A web spider wont download fiels but will just check the exist.
- v = verbose

Fine the process using a certain port
lsof -P | grep ':555'
Also a decent command for keeping an eye on what's going on
-P = specify the port nc should use

Share a file through port 80
nc -v -l 80 < file.txt
Fire this on one machine, and then on another machine go to 
http://<ip-address-of-first-machine> to get the file.
-v = verbose output.
-l = listen for an incoming connection, do not create one.
80 = HTTP port

List open TCP/UDP Ports
netstat -ltun
This is interesting...maybe clear up some security  issues..
-l = shows only listening sockets
-t = TCP ports
-u = UDP ports
-n = shows numeric addresses

Google Text-To-Speech in MP3
wget -q -U Mozilla -O output.mp3 
This has all kinds of uses.
-q = quiet (turns off wget's output)
-U =  "User Agent" (more information on the wget man page
-O = output all to one file

Set audible alarm when IP come back on-line 
ping -i 60 -a IP_address
This is good for when you working on another computer but need to know when a server is back up and running.

Go straight to home directory
cd ~
I used this today for the first time. I had no idea it existed, I was doing cd ../../../ :\ 

Get your external IP and Network Info
This is handy if you're in an SSH session, I've used it a few times at work..pretty decent.

Remove all files and directory's from a directory
rm -rf <DirectoryName>
Be careful with this, 'rm -rf' can be a killer if in the wrong hands
-rf = the 'r' is recursive, this means it will remove all files from all directory's and 'f' is force, which means ignore any prompts (i.e. are you sure you wish to delete this?)

CD to a directory, run a command and come back again
(cd /home/Zdrenka/Desktop && touch BOOM.txt)
I've used this for scripts mainly, its very quick!

Runs previous command but replacing
Really useful for when you have a typo in a previous command. Also, arguments default to empty so if you accidentally run:
echo "Hello Worldf"
you can correct it with

Runs previous command replacing "phrase" by "anotherphrase" every time that "phrase" appears

Very useful for rerunning a long command changing some arguments globally.

Shutdown a Windows machine from Linux
net rpc shutdown -I ipAddressOfWindowsPC -U username%password
This will issue a shutdown command to the Windows machine. username must be an administrator on the Windows machine. Requires samba-common package installed. Other relevant commands are:
net rpc shutdown -r
: reboot the Windows machine

net rpc abortshutdown 
: abort shutdown of the Windows machine

Kills a process that is locking a file.
fuser -k filename
Your Welcome ;) (p.s. this was a pain in the arse when I was using windows)

Remove all but one specific file
rm -f !(survivior.txt)
The other day I was merging a load of mp3 files into a single mp3, I used this command to remove everything but the single one I had created.

Create a 1280x720 plasma image
convert -size 1280x720 plasma:fractal background.png
Now this is a keeper, This comes in handy when creating logos or banners

Output your microphone to another computers speakers
dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp
Good for freaking people out 

Play musical notes
man beep | sed -e '1,/Note/d; /BUGS/,$d' 
| awk '{print $2}' | xargs -IX sudo beep -f X -l 500
Pointless but fun

Find out how old a webpage is
wget -S --spider 2>&1 | grep Mod
I suppose this /could/ be useful.... couldn't' it?
the -S -- spider means wget will not download the page but check to that it is there, and in this case retrieve some information.

Displays your distro
cat /etc/*release
This can be useful if you are on a remote machine.

Check the model of your computer
sudo dmidecode -s system-product-name
I have used this a few times, mainly on remote PC's so I know what I'm dealing with.

Generate a random password
openssl rand -base64 12
This is good if you spend hours trying to think of a password (as I do). 

Remove passwords from PDF's
for F in *.pdf ; do qpdf --password=your_password --decrypt
 "$F" "$(basename $F .pdf)-nopw.pdf" ; done
Remove those pesky passwords!

Figlet Ascii fun 
Figlet is a cool little command, with figlet you can get some cool Ascii graphics
Ideal for email signatures. you may need to install figlet first:
 sudo apt-get install figlet
figlet DoItWithLinux

Break up a file into small manageable chucks 
Files which are kinda big sometimes take quite a while to send over a network, well before you do try the following:
split –b500m myBigFile mySmallFIles
this will split them up into small chucks
The -b chooses the size of the chunks
to put them back together again:
cat mySmallFiles.* > myBigFile
the * is a wildcard which means all.

Execute a command at a set time
I have already coverd this one previously in the blog,
but its cool so you're going to have to see it again :D
echo “ls -l” | at midnight
Come on! you know what this does! just replace the 'ls -l' with the desired command/script.

Capture video of the linux desktop

ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/outputFile.mpg
For this command you may need to install
'ffmpeg', I did anyway :\
x11grab grabs the x11 display (your screen ratio and such)
-f = 'force format'
-r = frames per second so here we have chosen 25
-s = the resolution size
-i = the name of the file we wish to create
-sameq = uses the same video quality as source

Run the last command as root
sudo !!
This is a life saver, when you type out a huge command and then forget
to put sudo in front of it, especially bad when its a remote machine which lags :\

Spooky typing
echo "Follow the white rabbit" |pv -qL11
This reminds me of the matrix, hence the quote, this will output characters at 11 per second
pv is a command which allows you to see the progress of data through a pipeline ( | )
-qL = no output (as in no unwanted output) the L is the limit which in this case is 11 per second

Kick ass figlet clock
watch -t -n1 "date +%T|figlet"
I'll admit, it's a pretty pointless command but its fun!
Watch command is used if you wish to watch how something changes over time, in the case the actual time.
-t = no title (no title for the watch command).
-n = the amount of seconds (in this case 1).
the date command is self explanatory.
+%T = time without the date.

Record from remote computers microphone
ssh USER@REMOTESYSTEM arecord - | aplay -
Come on! how cool is this. This command records from a remote computers microphone and plays it back through your own speakers..with great power come great responsibility 
arecord is a command line sound file recorder, in this example it is recording sound from the remote computer.
aplay is pretty much the same only it plays instead of recording.

Diff files over SSH
ssh login@host "cat remote file" | diff - "local file"
This saves a lot of time messing about with ftp :\
Diff is a program which compares two files line by line.

run a local script over SSH
ssh -T user@server <
See above description

Stream audio over SSH
ssh [user]@[address] "mpg321 -" < [file].mp3
why file share?

Connect via SSH using MAC addresses
sudo arp -s 00:35:cf:56:b2:2g temp
 && ssh root@
useful if you have a dynamic IP
Instead of looking for the right IP address, just pick whatever address you like and set a static IP mapping.

Shuffle a list of MP3's and play them
ls | grep -i mp3 | sort -R | sed -e 's/.*/"&"/' | xargs mpg123
grep -i leaves only mp3 files
sort -R randomizes list (may use GNU 'shuf' instead).
the sed command will add double quotes around each filename (needed if odd characters are present)

Show full system info
inxi -F
All the information one could possible want

Remove all empty directory's
find . -type d -empty -delete
Good for saving time, especially if your a messy git like me

List the contents of a .jar file
jar -tf file.jar
As a Java developer this is pretty useful.

Google Translate speak through speakers
function say { mplayer -really-quiet 
"$1"; }
Put the string above in your ~/.bashrc or ~/.bash_profile, then source the file. Make sure your sound output is working, you have mplayer installed, then type in a word or sentence similar to below:
say "why won't anyone talk to me?"
It's easy to get the language to be different by changing the "en" in the string to be "de" or some other language that Google Translate supports. Have multiple "say" functions, like "say-en" "say-de".

Merge MP3's Together
cat file1.mp3 file2.mp3 > file3.mp3
Quite simple and it does the job.

Console Screen Saver
alias screensaver='for ((;;)); do echo -ne 
((RANDOM%10))"; sleep 0.1 ; done'
This just looks cool, try it out.. just paste this in and then type screensaver

More Coming guys!