DEV Community

Manvi Tyagi
Manvi Tyagi

Posted on

Linux Commands for beginners

Opening a terminal

Most Linux systems use the same default keyboard shortcut to start it: Ctrl-Alt-T

Moving Around

  1. pwd: pwd is an abbreviation of ‘print working directory’. All it does is print out the shell’s current working directory.
  2. cd: ‘change directory’
  3. ls: lists directory contents of files and directories
  4. Relative and absolute paths Relative Paths: The place you end up at depends on your current working directory. Absolute Paths: No matter what your current working directory is, they’ll have the same effect.
  5. cd: when you run cd on its own to go straight to your home directory.
  6. cd /: to switch to the root directory.
  7. Any path that starts with a forward slash is an absolute path. You can think of it as saying "switch to the root directory, then follow the route from there."
cd /etc  //goes to root directory first and then to etc
Enter fullscreen mode Exit fullscreen mode
  • There’s one other handy shortcut that works like an absolute path. As you’ve seen, using “/” at the start of your path means “starting from the root directory”. Using the tilde character (”~”) at the start of your path similarly means “starting from my home directory”.
cd ~
cd ~/Desktop
Enter fullscreen mode Exit fullscreen mode

Creating folders and files

  1. mkdir is short for ‘make directory’.
mkdir dir1 dir2 dir3 // create all the folders in one directory.
mkdir -p dir4/dir5/dir6 //create in nested structure 
Enter fullscreen mode Exit fullscreen mode
  1. Creating new files (a) Create a File with Touch Command
touch test.txt
Enter fullscreen mode Exit fullscreen mode

This creates a new empty file named test.txt

(b) Create a New File With the Redirect Operator
A redirection operator is a name for a character that changes the destination where the results are displayed.

Right angle bracket >

This symbol tells the system to output results into whatever you specify next. The target is usually a filename. You can use this symbol by itself to create a new file:

test2.txt
Enter fullscreen mode Exit fullscreen mode

This creates a new empty file.

(c) Create File with echo Command

echo ‘Random sample text’ > test4.txt
Enter fullscreen mode Exit fullscreen mode

Editing files

Linux file system allows us to operate various operations on files like create, edit, rename, remove. We can edit files by different Linux editors like vim, nano, Emacs, Gedit, Gvim, and more.
We will cover nano & vim for this article.
Difference between Nano and Vim
Nano is simple, vim is powerful.
If you only want to simply edit some textfiles, nano will be enough.

1. VIM

The Vi editor has various modes like normal mode, insert mode, command mode, line mode, and more.
How to switch a mode in Vi editor?

  • Press the ESC key for normal mode.
  • Press i Key for insert mode.
  • Press :q! keys to exit from the editor without saving a file.
  • Press :wq! Keys to save the updated file and exit from the editor.
  • Press :w test.txt to save the file as test.txt

Edit test.txt

vi test.txt
Enter fullscreen mode Exit fullscreen mode

This command will open the file with the Vi editor in normal mode. To switch it to insert mode press ESC key followed by i key. Place the cursor on your desired position and enter some text. To save the file and exit from the editor, press the 'ESC' key, followed by :wq!.

2. NANO

It is a built-in editor for Linux distributions.
In nano, no primary command is used to operate on the file. All the basic operations are displayed at the bottom of the editor. We can trigger them with a CTRL key, for example, to save the file press CTRL+O keys, to exit from the editor press CTRL+X key.

To edit a file with the nano editor, open the file from the directory where it is stored with the following command:

nano test.txt  
Enter fullscreen mode Exit fullscreen mode

This will open the test.txt file with nano editor. To edit the file, move the cursor and enter some text and press the CTRL+O keys to save the file.
Press CTRL+X keys to exit from the editor.

Top comments (0)