Posts Linux Startup Basic Commands Compilation
Post
Cancel

Linux Startup Basic Commands Compilation

  • These should be executed on a Linux machine, most of them should work via git-bash as well

  • Current Working directory

1
2
3
$ # print current working directory is used to see the current directory absolute path

$ pwd
  • Change directory
1
2
3
4
5
6
7
8
9
10
11
12
13
$ # This will take you to your home directory

$ cd


$ # This will take you one directory back (space is important)
$ cd ..

$ # This will take you to the file system root
$ cd /

$ # This will take you to the directory that you specified
$ cd <dir>
  • List files or directories
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ # This will list the current working directory content.
$ ls

$ # This will give additional info such as permission, owner, group, timestamp, file name
$ ls -l

$ # This is the most used command as it sorts the file by timestamp (t) and then displays in descending order (r).
$ ls -lrt

$ # (a) Is used to display hidden contents as well
$ ls -la

$ # This is used to list directory contents without actually traversing to that location
$ ls <dir>
  • Creating directory
1
2
3
4
5
$ # Make directory command is used to create directories
$ mkdir <dir>

$ # used to create directory of current date, date hyphen capital i
$ mkdir $(date -I)
  • Removing directory/file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ # Use rm with recursive attribute -r to remove directory
$ rm -r <dir>


$ # Use rm with force attribute to silently delete all files -rf
$ rm -rf <dir>


$ # Use this command with file name to delete it, it will ask for confirmation.
$ rm <file name>


$ # Use this command to delete file silently
$ rm -f <file name>

  • View file contents
1
2
3
$ # This will list the file contents on console, used only for files with less information.
$ cat <file name>

1
2
3
$ # This is used to view file or perform actions such as searching its content
$ less <file name>

1
2
3
$ # This can be used to view file content as well
$ cat <file name>

  • Edit file contents
1
2
3
4
5
6
7

$ # This is the simplest command used to edit file.
$ # once the editing is done user can use the control + o to save changes
$ # control + x to exit

$ nano <file name>

  • Moving data
1
2
3
4
$ # This will move the file from current location to temp folder

$ mv file1 /tmp/

  • Search File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ find <location> -n <file name>

$ find /home/test -n “dummy.text”

$ # Same as above “i” is used to ignore case when looking for the file
$ find <location> -in <file name>


$ #To search files only and ignore all errors
$ find -type f -n <file name> 2>/dev/null  


$ # To search directory only and ignore all errors
$ find -type d -n <dir name> 2>/dev/null  
  • Search file content
1
2
3
4
5
6
7
8
9
10
11
$ # Search for text in a specific file
$ grep “text” file name

$ # Search for text in all file in current folder
$ grep “text” *

$ # Search for text in a specific file name pattern
$ grep “text” file_name*

$ # Search for text in multiple files
$ grep “text” <file1>
  • search text1 or text 2
1
$ grep "text1\|text2" server.log
  • Search text across multiple files
1
$ grep “text” <file1> <file2> <file3>
  • Search for exact match in file
1
$ grep -w "text" <file>
  • Search for pattern with wildcard
1
2
3
4
5
6
7
8
$ grep “hello*ld” <file>

$ # Use the "." for a single character match. 
$ # If you want to get a list of all five-character English dictionary words starting with "h" and ending in "o" 
$ grep '\<h…o\>' <file>

$ # Use ‘B’ to show few lines before the search and ‘A’ to show few lines after the search
$ grep -B 3 -A 2 "started" server.log
  • Tail command
1
2
3
4
5
$ # To see last few lines of a file
$ tail file_name

$ # To see contents of file as they change
$ tail -f file_name
  • Less command
1
2
3
4
5
6
7
8
9
10
11
12
13

$ # To open a file and view contents

$ # Open file and select Shift + G to see it work like tail -f command, use Control + C to stop auto rendering of changes

$ # Press “:” Colon and then use ? <text> to search text from bottom to top

$ # Press “:” Colon and then use / <text> to search text from top to bottom

$ # Use “n” to find previous occurrence and shift + n to find the next occurrence

$ less file_name

  • File differences
1
2
3
$ # To see differences in first and second files
$ diff file_1 file_2

  • List all Process
1
2
3
4
5
$ # To see all running process
$ ps -ef

$ # To list specific process from the list
$ ps -ef | grep <process name>
  • Splitting the output
1
2
3
$ # Awk is used to split the output and pull out the specific sections separated by space
$ df -h | awk '{print $2}'

  • Creating/Extracting archive
1
2
3
4
5
6
7
8
9
10
$ # Tar is the most common command to create archives
$ # -c is used to create tar
$ # -v list files when creating tar
$ # -f archive file is local

$ tar -cvf test.tar <folder/file>


$ # -x is used to extract the tar
$ tar -xvf test.tar
  • Logging to another box
1
2
3
4
5
6
7
$ # ssh is used to connect to a remote server, by default it connects to port 22
$ ssh user@server


$ # -p is used to connect to a different port

$ ssh -p 44 user@server
  • Sending files to remote box
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

$ # scp is used to transfer files from local to remote 
$ # scp <file> user@server:<path>
$ # Example:

$ scp test.txt rahul.chandna@testbox:/tmp/

$ # scp can also be used to transfer files from remote to local
$ # scp user@server:<path> /tmp
$ # Example: 

$ scp rahul.chandna@testbox:/tmp/test /tmp/

$ # Use -r to transfer folders recursively from local to remote
$ # scp -r <dir> user@server:<path> 
$ # Example:

$ scp -r /tmp/test rahul.chandna@testbox:/tmp/
  • Checking server memory
1
2
3
4
$ # You can use less or cat the meminfo file to see server memory stats
$ less /proc/meminfo

$ cat /proc/meminfo
1
2
$ # You can use the top command to see all running process and server memory
$ top
  • Checking server version
1
2
3
4
5
6
$ # Check this file on Red Hat box
$ less /etc/redhat-release


$ # Check this file on SUSE box
$ less /etc/SuSE-release
  • Checking server storage
1
2
3
4
$ # df is used to see size of all disks
$ # -h is used to print in human readable form.

$ df -h
  • Checking folder size
1
2
$ # du is used to see size of files when used with ‘s’ it calculates the   total size of folder and ‘h’ is used to print it in human readable form
$ du -sh
  • Switch user
1
2
3
4
5
6
7
$ # This is used to switch to different user, this will ask for the user password
$ su - <username>


$ # If you have sudo access to run ‘su’ then you could switch to a different user using sudo and this will not ask for user password
$  sudo su - <username>

  • Download http files
1
2
3
4
[More Examples](https://rahulchandna.com/tags/wget/)
$ # wget is an important tool to check site availability/download any http content if you have the complete URL

$ wget <URL>
  • Clear console
1
2
3
4
5
6
7
8
$ # If you console looks cluttered, you could simply use the combination of control key and L to clear it.

$ # Cntrl key + L


$ # Same can be done by calling clear command
$ clear

  • Generate password hash
1
2
3
4
$ # This command is used to generate password hash that is then used in automated user creation scripts.

$ openssl passwd -1 <string>

  • Generate random string
1
2
3
4
$ # This command is used to generate random string that could be used as a password or a user key.
$ # the last digit ‘32’ in this case is used to define the length of the generated string

$ openssl rand -base64 32
This post is licensed under CC BY 4.0 by the author.