Linux Shell Basics

Help

Manual for any command

$ man ls
$ man mkdir
$ man touch

Navigation

Current location

$ pwd

List files on working directory

$ ls  
$ ls -l  
$ ls -l /etc/bin  
$ ls -lrt
$ ls -la | less
-lstands for a long list, meaning a more detailed view of the files and folders in alphabetical order.
-rstands for reverse, meaning that the list is organized in the reversed order.
-tstands for time, meaning that the list is not organized alphabetically but based on timestamps.

Change directory

$ cd .
$ cd /
$ cd /home

File manipulation

Make directory

$ mkdir new_dir
$ mkdir Year{2010..2020}

Delete directory

$ rm -d new_dir

Create new file

$ touch new_file.txt

Print out content

$ cat new_file.txt

Edit file content

$ nano new_file.txt
$ vi new_file.txt

Move or rename file or directory

$ mv new_file.txt new_file_name.txt
$ mv new_file.txt directory

Copy files and directories

$ cp new_file.txt /folder/new_file.txt

Filesystem hierarchy

/
Primary hierarchy root and root directory of the entire file system hierarchy.

/bin
Essential command binaries that need to be available in single user mode; for all users, e.g., cat, ls, cp.

/dev
Boot loader files, e.g., kernels, initrd.

/etc
Host-specific system-wide configuration files

/home
Users’ home directories, containing saved files, personal settings, etc.

/root
Home directory for the root user.

/sys
Contains information about devices, drivers, and some kernel features.

/tmp
Temporary files. Often not preserved between system reboots, and may be severely size restricted.

More about directory structure: https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard

Permissions

Read – having read permission on a file grants the right to read the contents of the file. Read permission on a directory implies the ability to list all the files in the directory.

Write – Write permission implies the ability to change the contents of the file (for a file) or create new files in the directory (for a directory).

Execute – Execute permission on files means the right to execute them, if they are programs. (Files that are not programs should not be given the execute permission.) For directories, execute permission allows you to enter the directory (i.e., cd into it), and to access any of its files.

NumberPermission TypeSymbol
0No Permission
1Execute–x
2Write-w-
3Execute + Write-wx
4Readr–
5Read + Executer-x
6Read + Writerw-
7Read + Write + Executerwx
Quick cheat sheet for Linux directory and file permissions
Owner (User)GroupPublic (Other)
Read (4)Read (4)Read (4)
Write (2)Write (2)Write (2)
Execute (1)Execute (1)Execute (1)
Chmod calculator: https://chmod-calculator.com/

Example task: Calculate Chmod
Owner: read, write, execute
Group: read, write
Public: read

Solution: r = 4; w = 2; x = 1
Owner: read (4), write (2), execute (1) – 4+2+1=7
Group: read (4), write (2) – 4+2=6
Public: read (4) – 4
Chmod = 764

Check file and directory permissions

$ ls -l
drwxr-xr-x 2 user user    6 Jan  7  2015 Desktop
-rw-rw-r-- 1 user user    0 Feb 16 14:17 file1
-rw-r--r-- 1 user wheel   0 Feb 16 14:22 file2

Directories are indicated by the d

Change file permissions

$ chmod u+x file_name
$ chmod g+rw file_name
$ chmod o-w file_name
$ chmod u=rwx,g=rx,o= file_name
$ chmod 774 file_name

I/O Redirection

Standard output
Redirect standard output to the file “>”. Append to the same file “>>”.

$ ls > file_list.txt
$ ls >> file_list.txt

Standard input

$ sort < file_list.txt

Filters

http://linuxcommand.org/lc3_lts0070.php#filters