symdesign
4/25/2017 - 6:22 PM

Basic CLI (command line interface) commands for Linux and Mac. This guide is based on: http://lifehacker.com/5633909/who-needs-a-mouse-learn

Basic CLI (command line interface) commands for Linux and Mac. This guide is based on: http://lifehacker.com/5633909/who-needs-a-mouse-learn-to-use-the-command-line-for-almost-anything

Basic CLI commands

Present working directory

https://gist.github.com/gfschultheiss/af6ac5f3684ad511def0f41943649a18 pwd shows the present working directory aka the directory you are currently in

Clear terminal

clear will clear the terminal viewport (the content is still available when you scroll up)

Command manual

man displays the manual for a command
h will open general help
q will quit the manual view

List files

ls displays a list of files inside the active folder. You can pass a number of parameters to the command to display extra details or change the sorting.

ls <parameters> <file or directory>
ls -lS relative/path/*.html

Parameters

-l show file listing in long format
-t sort the results by file time
-S sort the results by file size
-r reverse the sorting
-a also show hidden files
-d */ only show directories

You could use a combination of these together, like this command, which will show all files sorted by file size with the largest files at the bottom:

ls -lSr

Long format listing

When ls gets executed with the -l flag a result could look as follows:

drwx------    4 Friedrich  staff   136 Jul 14  2016 Applications
drwx------@  24 Friedrich  staff   816 Apr 25 13:25 Desktop
drwx------@  13 Friedrich  staff   442 Mar 29 11:46 Documents
drwx------+  52 Friedrich  staff  1768 Apr 25 14:26 Downloads
drwx------@ 101 Friedrich  staff  3434 Apr 24 14:50 Library
drwx------+   5 Friedrich  staff   170 Apr 22 15:51 Movies
drwx------+   5 Friedrich  staff   170 Sep  8  2016 Music
drwx------+  14 Friedrich  staff   476 Apr 22 12:34 Pictures
drwxr-xr-x+   2 Friedrich  staff    68 Jan 16  2013 Public
drwxrwxrwx+   3 Friedrich  staff   102 May 25  2016 Sites

Permissions, Hardlinks, Owner, Group, Size, Time, Name

Permissions

The first symbol describes the type of the list item.

d item is a directory
- item is a regular file

This is followed by three times rwx which defines the permissions for

  1. the file's owner
  2. members of the file's group
  3. everybody

If one of the letters is replaced by - then the according permission is not granted.

r item can be read
w item can be written
x item can be excecuted

You can also often see permissions described by three numbers e.g. 755 or 777. Again each digit defines the permissions for the User, Group or Everybody. It is composed by adding up the numbers 4, 2 and 1

r = 4
w = 2
x = 1

Change directories

cd allows you to change directories. You can navigate to either full or relative paths.

cd /absolute/path/
cd relative/path/

To swap directories to the previous working directory, the - (hyphen) shortcut is handy to have on hand.

cd -

To go to the user's home directory ~ (tiilde) can be used.

cd ~

Edit Plain Text Files

If you're using Ubuntu Linux or Mac, you can use the nano editor to quickly edit files.

nano /path/to/file

Otherwise, the vim editor is available on just about any system and can be invoked with the vi syntax.

vi /path/to/file

To quit vim exit the "-- INSERT --" mode by pressing ESC and then write ":w" to write and ":q" to quit.

Create or remove folder

To create a new folder, you can simply use the mkdir <foldername> command. You can then remove any folder with the rmdir <foldername> command—as long as the folder is empty. If there are files in the folder, you'll have to delete those files before you can remove the folder.

Create or remove file

touch <filename>creates a new, blank file
rm <filename> deletes files

You can quickly remove all files in a directory by using the '*' (asterisk) wildcard.

rm *

If you want to delete a list of files and folders, including all files from subdirectories, without prompting you for every single entry, you can use the -r option for recursive, and the -f option for force.

rm -rf filename.*

Display files

cat or more display the file contents directly on the screen.

less

less displays the contents of a file on the screen, and prompt you to scroll through the file a screen at a time. This is useful when the file is too long for the terminal to be displayed with cat or more.
let you navigate up and down line by line
space lets you go downwards page by page
B lets you go upwards page by page
Shift G brings you to the last page
g brings you to the first page

/keyword lets you search inside the file up to down
?keyword lets you search inside the file down to up
n brings you to the next found keyword

q quits the less command

Create files

For small files cat is often easier than using vi, nano or other text editors. For example, a new file named filename can be created by typing.

cat > filename

Command redirection

| pipes an output to another command
> saves an output to a file
>> appends an output to a file

cat filename.list | grep keyword > filefound.list

Create a new file file1 that consists of text typed in from the keyboard followed by the contents of file2

cat - file1 > file2

Create a new file file3 that consists of the contents of file4 followed by text typed in from the keyboard

cat file3 - > file4 

File concatenation

When leaving the '|' pipe aside the cat command will concatenate multiple files.

cat file1 file2 file3

Run a Script in the Current Folder

If you have an application or shell script in the current folder, you can't simply type the name of the command and expect it to start. You'll need to add a ./ to the beginning of the command in order to start it. Why? Because in the Bash shell, the current directory, or "." folder, is not included in the system path.

./scriptname.sh

Ctrl R will start a search mode where you can type the first few characters of a command to search through your recent history or use the up/down arrows to loop through them.

Loop Over a Set of Files

for f in *.txt;do echo $f;done