Linux Fundamentals 1

Linux is a operating system like OS X and Windows, however much more lightweight. It is built upon UNIX, which is an open-source operating system. Two of the most common distribution of Linux are Ubuntu and Debian.

Useful links:

Let’s get started with some of the most useful commands in Linux:

echo - Outputs text to the terminal.

1
2
user@linux:~$ echo "Hello, World!"
Hello, World!

whoami - Prints the username of the current user.

1
2
user@linux:~$ whoami
user

man - Displays the manual page of a command.

1
user@linux:~$ man ls

Interacting with Files and Directories

ls

  • Lists all files and directories in the current directory.
    • To list files and directories in long format with human-readable file sizes:
      • ls -lh (current directory)
      • ls -lh /usr/bin (specific directory)
      • ls -lh *.txt (specific file type)
      • ls -lh file* (specific file name)
      • ls -lh /usr/bin > file1.txt (redirecting the output to a file)
      • ls -al (lists all files and directories, including hidden ones)
      • ls -lASh (lists files and directories in long format, sorted by size, and human-readable file sizes)
Option Description
-a Lists all files and directories, including hidden ones.
-l Lists files and directories in long format.
-h Makes file sizes human-readable.
-t Sorts files and directories by modification time.
-lh Lists files and directories in long format with human-readable file sizes.
-F Appends a character to the end of each file name to indicate the type of file.
-R Lists files and directories recursively.
-S Sorts files and directories by size.
-A Lists files and directories, excluding . and ...

cd

  • Changes the current directory.
1
2
user@linux:~$ cd Documents
user@linux:~/Documents$
  • cd .. can be used to move up one directory.
  • cd can also be used to move to absolute paths, like cd /usr/bin.

pwd

  • Prints the current working directory.
1
2
user@linux:~$ pwd
/home/user

mkdir

  • Creates a new directory.
    • mkdir new_directory creates a new directory named new_directory.
Option Description
-p Creates parent directories as needed. mkdir -p directory1/directory2/directory3
-v Displays a message for each directory created. mkdir -pv directory1/directory2/directory3
-m Sets the permissions for the directory. mkdir -m 777 new_directory

rm

  • Removes files or directories. It does not move files to the trash, it deletes them permanently.
1
user@linux:~$ rm file1.txt
Option Description
-r Removes directories and their contents recursively.
-f Removes files without prompting for confirmation.
  • To remove a directory and its contents: rm -r directory1
  • To remove multiple files: rm file1.txt file2.txt
  • To remove all files with a specific extension: rm *.txt
  • To remove all files starting with a specific name: rm file*
  • To remove all files in a directory: rm *

rmdir

  • Removes empty directories.
    • rmdir * removes all empty directories in the current directory.

cp

  • Copies files and directories.
  • cp file1.txt directory1 copies the file to the directory.
  • cp file1.txt file2.txt copies the file.
  • cp -i file1.txt directory1 prompts for confirmation before overwriting files.
  • cp -r directory1 directory2 copies directories and their contents recursively.

mv

  • Moves files and directories.
    • mv file1.txt directory1 moves the file to the directory.
    • mv file1.txt file2.txt renames the file.
    • mv directory1 directory2 moves the directory.
    • mv -i file1.txt directory1 prompts for confirmation before overwriting files.

touch

  • Creates an empty file.
    • touch file1.txt creates a new file named file1.txt.

cat

  • Reads and displays the contents of files sequentially.

For example: To read contents of a file named file1.txt and file2.txt:

1
2
user@linux:~$ cat file1.txt
This is file1
  • to display line numbers with the contents of the file, use the -n option. cat -n filename
  • to concatenate the contents of two files and store them in a new file, use the > operator. cat file1.txt file2.txt > file3.txt
  • to append the contents of a file to another file, use the >> operator. cat file1.txt >> file2.txt
  • to display the contents of a file in reverse order, use the tac command. tac filename

find

  • Searches for files and directories.
1
2
user@linux:~$ find / -name file1.txt
./home/user/Documents/file1.txt
  • To search every txt files: find / -name "*.txt". Remember to use quotes.
  • To search files whose name starts with fi: find / -name "fi*"

grep

  • grep shorts for Global Regular Expression Print. It searches for a pattern in a file or files.

let’s we have a file named file1.txt with the following content:

1
2
This is file1 with example content. 
this is the line 2. 

To search for the word example in the file, use the following command:

1
2
user@linux:~$ grep "example" file1.txt
This is file1 with example content.
Option Description
-i Ignores case.
-n Displays line numbers.
-c Displays the count of matching lines.
-r Searches recursively in directories.
-lr Displays only the names of files with matching lines, recursively.

head and tail

  • head Displays the first few lines of a file.
    • head file1.txt displays the first 10 lines of the file.
    • head -n 5 file1.txt displays the first 5 lines of the file.
  • tail Displays the last few lines of a file.
    • tail file1.txt displays the last 10 lines of the file.
    • tail -n 5 file1.txt displays the last 5 lines of the file.

less

  • Displays the contents of a file one page at a time.
    • less file1.txt displays the contents of the file.
    • Press q to exit the less command.

Introduction to shell operators

&

  • Runs a command in the background. command &

&&

  • Runs a command only if the previous command was successful. command1 && command2

>

  • Redirects the output of a command to a file. If the file already exists, it is overwritten. command > file1.txt

»

  • Appends the output of a command to a file. If the file already exists, the output is appended to the end. command >> file1.txt

|

  • Pipes the output of one command to another command.
    • command1 | command2 pipes the output of command1 to command2.
    • ls -l | grep ".txt" lists all files in the current directory and pipes the output to grep to search for files with the .txt extension.
    • ps aux --sort=-%mem | head -n 5 lists the top 5 processes consuming the most memory.
    • du -h | sort -rh | head -n 5 lists the top 5 directories consuming the most disk space.

File Permissions in Linux

  • In Linux, each file and directory has three types of permissions: read, write, and execute. These permissions are assigned to three entities: the owner, the group, and others.

  • To view the permissions of a file or directory, use the ls -l command.

1
2
user@linux:~$ ls -l file1.txt
-rw-r--r-- 1 user user 0 Jun 12 00:37 file1.txt
  • - indicates a regular file. rw- indicates that the owner has read and write permissions. r-- indicates that the group has read permissions. r-- indicates that others have read permissions.

  • Numeric values are used to represent permissions.

    • r is represented by 4, w is represented by 2, and x is represented by 1.
    • rwx is represented by 7, rw- is represented by 6, r-x is represented by 5, and r-- is represented by 4, --- is represented by 0.

Some Basic Permissions:

Symbolic Representation Numeric Value Description
rw-r–r– 644 Read and write permissions for the owner, read permissions for the group and others.
rwxr-xr-x 755 Read, write, and execute permissions for the owner, read and execute permissions for the group and others.
rwxrwxrwx 777 Read, write, and execute permissions for the owner, group, and others.
rw-rw-rw- 666 Read and write permissions for the owner, group, and others.
rwx—— 700 Read, write, and execute permissions for the owner only.
r——– 400 Read permissions for the owner only.
r-x—— 500 Read and execute permissions for the owner only.

chmod

  • Changes the permissions of a file or directory.
    • chmod 777 file1.txt grants read, write, and execute permissions to the owner, group, and others.
    • chmod 755 file1.txt grants read, write, and execute permissions to the owner and read and execute permissions to the group and others.

chown

  • Changes the owner of a file or directory.
    • chown user1 file1.txt changes the owner of the file to user1.
    • chown user1:group1 file1.txt changes the owner and group of the file to user1 and group1.

Common Directories

  • /etc - Contains system-wide configuration files.
  • /var - Contains variable data files, such as logs and databases.
  • /tmp - Contains temporary files. Once the system is rebooted, the files are deleted.
  • /root - Home directory of the root user.

Related Content

0%