Linux Cheatsheat

Basic Commands Table

cdChange directory
lsDirectory listing
ls -alFormatted listing with hidden files included.
cd dirChange directory to dir
pwdPrint working directory
mkdir dir1 dir2Creates directory dir1 and dir2
rm fileDelete file
cp file1 file 2Copy file1 to file2
mv file1 file2Rename or move file1 to file2. If file2 is an existing directory, move file1 into directory file2.
psDisplays all currently active processes
topDisplays all running processes
kill pidKills process id pid
bgLists stopped or background jobs
chmod octal fileChange the permissions of a file to octal, which can be found separately for user, group, and world (see below)
ssh user@hostConnect to a host as a specified user
ssh -p port user@hostConnect to a host on target port as a specified user
ping hostPing target host and output results
whois domainGet whois information for a target domain
dig domainGet DNS information for a target domain
dig -x hostReverse lookup for target host
tar cf file.tar filesCreate a tar named file.tar containing files
tar xf file.tarExtract the files from file.tar
tar czf file.tar.gz filesCreate a tar file Gzip compression
tar xzf file.tar.gz / gunzip file.tar.gzExtract a tar using Gzip
tar cjf file.tar.bz2Create a tar using Bzip2 compression
tar xjf file.tar.bz2Extract a tar using Bzip2
gzip fileCompresses a file and renames it to file.gz
gzip -d file.gzDecompresses file.gz back to file
whoamiDisplay who you are logged in as
wDisplay who is online
finger userDisplay information about user
uname -aShow kernel information
cat /proc /cpuinfoDisplay CPU information
cat /proc /meminfoDisplay memory information
man commandShow the manual for specified command
dfShow disk usage
whereis appShow possible locations of app
locate fileFind all instances of file
grep pattern filesSearch for a pattern in files
grep -r pattern dirSearch recursively for a pattern in dir
command | grep patternSearch for a pattern in the output of command

Directories

The mkdir (make directory) command requires administrative privileges. Directories can be created in batches by entering the directories in one command. As an example, to make the directories: “arabica” and “robusta” you can execute the command mkdir arabica robusta. The full path must be entered or both directories will be created in the current directory. For example, mkdir arabica creates the arabica directory in the current directory, whereas mkdir /usr/arabica will create arabica in the /usr directory regardless of your current directory.

There are a few different options for the mkdir command that are pretty common. The -p option creates all parent directories if they do not already exist. The -v option sets the output to print for each created directory. The -m option enables the user to set permissions for the directory as it is created. For example, in octal mode, mkdir -m 777 /usr/arabica/Caturra will enable owner, group, and world to read, write, and execute permissions to the Caturra directory.

Permissions

Alphabetic symbols to represent roles and permissions assigned to each role.

chmod

The symbol to grant permissions is (+) and the symbol to remove permissions is (-). For example, to give a group permission to read, write, and execute on the /usr/arabica/Bourbon/Caturra directory, run the following command: chmod g+rw /usr/arabica/Bourbon/Caturra.

Permissions can also be changed using octal modes. Octal modes are numeric representations of permisions. read = 4, write = 2, and execute = 1. So if you wanted to give someone full read, write and execute permissions, use the number 7 (4+2+1). To give read and write permissions only, use 6 (4+2). To give read only permissions, use 4. Octal mode also requires a number for each role (user, group, other users). For example, to give the user read, write, and execute permission to the /usr/arabica/Bourbon/Caturra directory, run the following command: chmod 740 /usr/arabica/Bourbon/Caturra.

To remove (-) the ability of all users (o) to read (r) and execute (x) files in the Caturra directory: sudo chmod o-rx Caturra [notice this is a relative path (as opposed to an absolute, or full path) command using sudo to elevate privileges].

Likewise, to remove (-) the ability of group members (g) to read (r) and execute (x) files in the Caturra directory: sudo chmod g-rx Caturra.

Further octal examples: chmod 777 – read, write, and execute for all. chmod 755 – rwx for owner, rx for group, and world. For further info, see man chmod.

chown

The command chown is used to change the owner and/or group of a file. The user and group can be changed at the same time, by using either the . or : symbols. For this next example, let’s use the colon symbol (:). To change both the owner (to Catuai) and the group (to Typica) of the /usr/arabica/yellow directory at the same time, we can execute the following command: chown Catuai:Typica /usr/arabica/yellow. To change all of the files or directories under /usr/arabica directory, including yellow, type: chown -R Catuai:Typica /usr/arabica. And finally, to change the group to Typica without changing the owner, simply type: chown :Typica /usr/arabica. This doesn’t specify the owner name.

Type chown --help | more for the onscreen manual that’ll explain the purpose of this command.

chgrp

chgrp (change group) command changes the group that has access to a file. The chgrp command is similar to the chown command, however, chgrp can change only the group associated with a file or directory. To change the group owner of Caturra from root to the group huehuetenango: sudo chgrp huehuetenango Caturra.

ls -al

-rw-r–r– 1 root root 16 Sep 27 10:09 sl34.txt

The sample output text seen above shows a lot of useful information. Next to the filename (sl34.txt) shows the last time the file was modified: 10:09, followed by the date the file was last modified: Sep 27. The next item displayed is the size of the file (in bytes): 16. The group, root, and the owning user, root, follow. Next is the number of hard links to that location. In this case, it is 1. Finally, all the way to the left, is a reference to the file permissions that are represented by symbols.

File permissions either start with a dash (-) for files or the letter d (for directories). This example references a text file, so it begins with a dash. The next three symbols (rw-) represent the permissions assigned to users; those are read (r) and write (w), meaning the root user can read and write to this file. The second group of three symbols represent group permissions; here, all users in the root group are only permitted to read (r) the file. The last three symbols in the string represent the permissions for the world, or all other users; in this case, world users are permitted only to read (r) as well.