Simplifying Linux Part - 1 Introduction to Linux commands

Simplifying Linux Part - 1 Introduction to Linux commands

Β·

6 min read

I am learning DevOps for a while and I will be writing about everything I learn, which will eventually be beneficial for the community and aspiring DevOps learners. I previously wrote about YAML which is very important for the DevOps journey have a look here.

Prerequisite

  • 1. A Linux terminal
  • 2. Enthusiasm for learning

For setting up Ubuntu in windows terminal head over to this article officially by Ubuntu.

Okk...Let's start with shell, this is the place where we write our commands but what it is?? A shell is a command-line interface that will take all our command as input, interprets them, and converts them into useful things.

For some commands I have not included screenshot as they have to done by self.

Screenshot (2).png Here's an image of what shell looks like.

Shells are of many types like Bourne shell,c-shell,z-shell, etc but here we won't discuss that as it is a very different topic, but as always you can head over here to know about it.

Let's deep dive with the commands -

  • $ clear ``` - Clears the shell.

  • $ ls

    • This command lists all files and folders in the current directory.

Screenshot (3).png

  • **$ mkdir** - Makes new folder

Screenshot (4).png

  • $ cd - This one changes the directory if we write ```

cd .. ``` then this takes us one folder back.

  • $ whereis - This gives the exact directory of any file.

  • $ xdg-open - Opens particular directory.

  • $ ls -a - Shows hidden files and folders.

Screenshot (5).png

  • $ pwd - This prints the current working directory.

Screenshot (6).png

. -> current directory .. -> previous directory

  • $ ls -l - Gives more arguments like writable, readable, and executable state of the file, permission, and stuff.

Screenshot (7).png

  • $ ls -R - Lists all-recursive folder i.e folder inside folder .... and so on

  • $ cat - This outputs the content of a file.

Screenshot (8).png

If there is no file then $ cat > hello.txt can be used to make a file and give inputs to it.

Also $ cat file.txt hello.txt > sumit.txt can be used to replace the content of sumit.txt with the other two. $ echo "Hello World" > file.txt this replaces all content of file.txt with Hello-World.

  • $ man - This command gives information about a particular command. If you specify $ man sudo in the terminal you will get something like this πŸ‘‡

Screenshot (9).png

  • $ touch - touch command create a new file.

$ touch new.txt

  • $ cp - copy contents of one file to another.

cp file.txt newFile.txt

  • $ mv - this can rename as well as move a file.

mv fike.txt newName.txt (This renames)

mv file.txt usr/local/bin (This copies)

  • $ rm - Remove a file

rm file.txt

  • $ rmdir - Removes a directory only if it is empty.

$ rmdir

  • $ sudo - This command gives administrative privileges, if you are a Linux user you may be fully aware of it.

  • $ df - gives a capacity of the disk.

df -m (Size in Megabytes)

df -k (Size in Kilobytes)

df -h (In human-readable form)

Screenshot (11).png

Screenshot (10).png

  • $ du - This gives disk usage statistics.

du -l

du -h (In human-readable form)

Screenshot (12).png

  • $ head - Used to vies first few lines of any file, by default, Screenshot (13).png it's 10.

As you can see after using the head command it's only showing 10 lines. $ tail command shows the last few lines.

  • $ diff - This differentiate content of a file.

  • $ locate - This locates any file. $ locate ".txt" this will locate all the files which have .txt extension.

  • $ whoami - Displays username.

Screenshot (14).png

  • File Permissions - There are 3 file permissions Read, Write and Execute

$ chmod is the command to change file permissions.

Performing action on multiple files

If you wanna let's say remove multiple files at once then use:-

$ find . -type f -name "*.txt" -exec rm -rf {} +

basically {} is the placeholder and rm shows that remove these.

  • $ grep - Global regular expression print, grep allows to look for particular text in the file. GREP is case-sensitive. I have made a text file that contains names of different random people and applied the following commands πŸ‘‡

$ grep "Kunal" names.txt

$ grep -w "Kunal" names.txt (Shows complete word)

$ grep -i "kunal" names.txt (shows case sensitive names too)

$ grep -iw "kunal" names.txt (shows case sensitive names with word specified)

Screenshot (15).png

$ grep -n "Kunal" names.txt (Shows line number)

$ grep -win "Rahul" names.txt (Combined with the word, case sensitive, and line number)

$ grep -b 3 "Rahul" ./.*txt" (WIll search for Rahul in all txt files)

$ grep -rwin "Rahul" ./.*txt (Will search recursively in all directories)

Screenshot (16).png

Terminal Shortcuts -

  1. Ctrl + A - Moves the cursor to begin.
  2. Ctrl + E - Moves the cursor to end. 3 Ctrl + U - Removes Everything written.
  3. Ctrl + K - Removes a word after the cursor.
  4. Up and Down Keys - Previous commands.
  5. $ ! - With ! sign it will run the previous command.
  6. $ command_1;command_2;command_3 - Multiple commands with semi-colon.
  7. $ sort names.txt - will sort in Ascending order.
  8. $ sort -r names.txt - will sort all the names in reverse order.
  9. $ jobs - displays current job.
  10. $ ping - Check your server.
  11. $ wget - Download file.
  12. $ top - shows all the running processes. 14 . $ kill - Kills the running process.
  13. $ zip file_name.zip file_to_be_zipped - Zips the file

if this doesn't work then use $ sudo apt install zip (installs package). Multiple files can be zipped.

  1. $ unzip file_name.zip - Unzipes the file
  2. $ hostname - Displays hostname.
  3. $ hostname -i - Displays IP too
  4. $ uname - Gives OS name
  5. $ uname -m - Gives Architecture of os.
  6. $ uname -r - Gives Kernal version.
  7. $ cat /etc/os-release - Gives Operating System Info.
  8. $ free -h - Displays free memory.

Networking Commands -

  • $ nslookup google.com - Gives IP address of the particular site, here google.com .
  • $ netstat - Looks all active stat.

Some more useful commands -

  • $ cut -c 1-2 names.txt - Gives names after cutting two letters and we can specify the condition as we need.

Screenshot (18).png

  • $ ps aux - Gives all the processes and it'd ids.

So this was it from my side and there are tons of commands but you will get everything while learning. Things will come naturally.

If you have read till here..then thanks. Keep Learning😊😊

Β