Lets Make Tech

Useful Commands Every Linux User Should Know

The linux command line is a powerful interface that when used correctly can save you a great deal of time. I compiled a list of commands to use as a reference sheet.

These commands are powerful and can be used alone or to make BASH scripts that you can set to run automatically with a cron job.

A good reference on how to make a BASH script with these commands http://linuxcommand.org/writing_shell_scripts.php#contents 

 

System manual

man


Running multiple commands (eg. cd && ls)

&&


Previous Commands

Runs the previous command

!!


View previously executed commands

history


Navigation
Print the working directory

pwd 


Used to display contents of current directory.

ls

Useful options for ls:

Displays all files.

 

ls -a

Shows links, file, owner, and group.

ls -al 

 

Goes to the users home directory or a directory specified 

cd
cd /var/www


**Copying**

Used to copy files

cp


Useful options for cp: 

cp -f forces it.
cp -r recursive
cp -s makes a symbolic link
cp -u updates file
cp -i prompt before overwriting
cp -p preserves some traits of file (mode,ownership,timestamps)


Moving files and Syncing
Moves file. /example file.txt /example/newlocation

mv


Program to sync locations 

rsync
rsync ~/Documents/server /var/www
Useful options for rsync:
rsync -a ~/Documents/server /var/www a preserves symbolic links and other attributes 
rsync -v ~/Documents/server /var/www verbose
rsync -r ~/Documents/server /var/www recursive 
rsync -z ~/Documents/server /var/www compresses

Finding
Finds files

find
find options:
-name
-iname (ignore case)
-d directory
-f files
-perm 0XXX (replace XXX with permissions
-print prints name
-exec execute a command to files found such as rm

Finds keyword in a file.

 

grep
Some uses of grep include:
grep 'word' filename
grep 'word' file1 file2 file3
grep 'string1 string2' filename

Find where the files associated with a command are located (replace cd with the command you want)

whereis cd

 

Reading Files
Display the contents of a file

cat examplefile.txt


Display the last portion of a file (useful for logs)

tail examplefile.txt

**Writing Files**

Update time stamp

touch examplefile.txt

Terminal text editors

nano examplefile.txt
vim examplefile.txt

Echo to location

echo example > example.txt 

 

Managing Files
Change permissions. Replace the XXX with numbers. The first X is user, the second X is group, the third X is world. 1=execute 2=Write 4=Read. Numbers may be combined.

chmod XXX examplefile.txt

 

Owner of files

chown owner-user file
chown owner-user:owner-group file
chown owner-user:owner-group directory
chown options owner-user:owner-group file Changes owner and group of a file

Delete files 

 

rm
useful options with rm:
rm -r 
rm -f

Compare two files 

cmp

 

Get System Info

uname
Useful options: 
-a all
-m machine
-n node
-p processor
-r kernel release
-s kernel name
-v kernel version
-i hardware platform
-o operating system

 
List devices

lsblk


Get network information

ifconfig 
Useful options for ifconfig:
ifconfig eth0 down disable
ifconfig eth0 up enable
ifconfig eth0 192.168.1.XX Replace XX with desired IP
ifconfig eth0 netmask 255.255.255. change netmask
ifconfig eth0 broadcast 192.168.1.255 change broadcast

Shows uptime 

uptime 


Shows work load

top


Shows network activity

iftop


Shows memory usage

free 
Useful options for free:
-b bytes
-k kilobytes 
-m megabytes
-g gigabytes

Disk Usage

df

Directory Size

du

Show system processes

ps -a

 

Kill a process (replace 1234 with the pid number by using ps)

kill 1234

 

If you know the name of the process you can use the following

pkill name_of_process

 

Start, restart, and stop services

service service_name start
service service_name restart
service service_name stop

Change Password

passwd

 

Shortcuts

Shorten the name of commands

alias la='ls -a'

Remove the alias

unalias la

 

Network

Download a file from the internet

wget www.website.com/directory/filename.fileextention

Ping a website

ping www.example.com

Configure networks

ifconfig

Get MAC address/ip/etc 

ifconfig -a

Follow the packet!

traceroute ip_address_goes_here

tracepath ip_address_goes_here

Packet sniffer

tcpdump

See if ports are open

netstat

See what ports are open on a machine

nmap

Enable a connection 

ifup eth0

Disable a connection

ifdown eth0

Configure a particular connection

ifcfg

View/Modify routing table

route

 

Wild Cards

Stand in for any one unknown character

?

Stand in for multiple unknown characters 

*

Wild Card & Regular Expression specify a range of characters

[ ] 

[a,e,i,o,u] 
[a-f]

Specify multiple terms

{*.jpg, picture.*}

Not

[!]
[!important]

Escape character if you want the following symbol to be treated as a normal symbol

/

Regular Expressions 

Stand in for any one unknown character

.

Regular Expression Match previous symbol zero or more times (stuf* would match stuff and stufff but not stufg)

*

 Combine the previous to to stand in for multiple unknown characters 

.*

Specifies first line

^ 

Specifies end of line

$

OR

|

Not

[^]

Escape character if you want the following symbol to be treated as a normal symbol

/

Uppercase letters

[:upper:] 

Lowercase letters

[:lower:] 

Alphabetic (letters) meaning upper+lower (both uppercase and lowercase letters)

[:alpha:] 

Numbers in decimal, 0 to 9

[:digit:] 

Alphanumeric meaning alpha+digits (any uppercase or lowercase letters or any decimal digits)

[:alnum:] 

White space meaning spaces, tabs, newlines and similar

[:space:] 

Graphically printable characters excluding space

[:graph:] 

Printable characters including space

[:print:] 

Punctuation characters meaning graphical characters minus alpha and digits

[:punct:]

Control characters meaning non-printable characters

[:cntrl:] 

Characters that are hexadecimal digits.

[:xdigit:] 

 

Now to really make use of all of this, you will want to learn how to write a BASH shell script. I will be sure to get a tutorial up soon on how to make your first BASH script.

 

 

How to Install Ratpoison Desktop On Ubuntu

If you have been a Linux user for pretty much any amount of time, you know that the desktop environment you use can be changed fairly easily. If you are looking to try a different desktop environment, or decided to add one to a Linux system you are setting up from scratch try Ratpoison. To learn about its history, check out this page.

Ratpoison is made with the goal of killing your mouse. Lets face it, mice are user friendly, but a keyboard (in the right hands) is way more efficient. Ratpoison does not do away with your mouse, you can still use it and all your GUI programs that rely on a mouse. 

Ratpoison allows you to tile your windows into whatever kind of grid you want and re-size them how you please. It also allows you to have other windows up, but out of sight.  

This desktop environment is good if you are working on a lot of code, or using a lot of terminals. 

undefined

(This blog started as johnblood.com, seen in the picture above)

First lets install it. 

sudo apt-get install ratpoison

Now lets create a desktop file for it so we can see it at the login screen.

sudo nano /usr/share/xsessions/ratpoison.desktop
[Desktop Entry]
Name=Ratpoison
Comment=This session logs you into Ratpoison
Exec=/usr/bin/ratpoison
TryExec=/usr/bin/ratpoison
Icon=
Type=Application
X-Ubuntu-Gettext-Domain=ratpoison-session

Alright, it is all set. Log out, then back in under the Ratpoison desktop

You will be greeted a a friendly welcome instructing you that your esc key is ctrl+t. If you were using ubuntu 14.04, you will still have your old wallpaper. in older versions of ubuntu, you would get a black screen.  \

Hit ctrl+t notice your mouse just changed? Good! Now it is ready for us to send it a command.

Now press c

You should have a terminal now, good job.

Now for some other commands we can do after hitting ctrl+t:

Commands

! Run program

. list programs in a menu

w list open windows. Note the corresponding numbers, enter a number after hitting ctrl+t to go to that window. 

n next window

p previous window

a tell the time

k kill

s split horizontal 

S split vertical 

u undo

ctrl+t switch back to the last window

: enter a command 

Commands after : 

quit logs you out

escape set a new escape key eg. escape C-a

bind bind commands to keys eg bind a exec chromium-browser

Save Settings

To keep these settings every time you login, you need to edit .ratpoisonrc in your home folder

# indicates a comment

exec starts a program

bind same usage as bind listed above

echo can be used to make a nice start up message, to disable the existing one with startup_message off

If you are having trouble, check out a few of these resources:

nongnu

PC Linux OS Magazine

The IT Crow

 

Home ← Older posts