Inspecting through Basic Commands in Linux - Day 1


Command Line Basics
Command Line Components
The commands that you type on your fancy linux terminal normally consists of 3 components:
Command: The name of the program that you want to execute
Options: As the name suggests, these are options that can be passed to the command to perform variety of tasks
Arguments: These act as a critical metadata for the options that they are being passed to in the command
There are plenty of commands that don’t have options, arguments or neither
sudo
Sudo refers to superuser do. sudo commands give admin privileges to the user while runtime of the commands. In ubuntu sudo is setup during the installation time, in other distros you might need to put your laziness aside and configure it manually.
Virtual Terminals
Now this is something cool that you can showoff. If you are like me and use an ubuntu system, you can simultaneously open another session that is different from what you are running here.
Virtual Terminals (VT) are basically console sessions that take up you complete screen and keyboard outside of a graphical environment. These are not the terminal that you start inside your gui after you start your ubuntu, you can have as many of them as possible
These are called virtual because, although there can be multiple active terminals, only one will be visible to you. Normally, one virtual terminal (either VT1 or VT7) is being reserved for the GUI of your linux based OS
To switch betwee VTs, you can press Ctrl + Alt + Function
key. If you are in ubuntu like me, the place where you see this blog is in VT1. You can switch to another VT by Crtl + Alt + F3
or F4 or F5…. To switch back, you’ll need to press Ctrl + Alt + F1
or F2 again
Basically the function keys are assigned to the VTs, the number of the function key maps to the number of the VT that you’ll use
Now, you can showoff
Turning off your GUI
There’s a fancy GUI developed by smart bros so that you can use your linux distros better. In new operating systems, the GUI can be easily turned off or on as it is treated as a service. In day 0, I wrote about how to turn off a service right? Yes, the systemctl
command.
To turn on or off your GUI, you can use these simple set of commands
# For turning off
sudo systemctl stop gdm
# For starting the gui
sudo systemctl start gdm
Basic command line operations
Login
Use the ssh
command to login to your system. e.g: ssh hello@world
Shutdown and restart
Use shutdown -h now
to shut down your pc immidiately
Use shutdown -r
to reboot
Locating Applications
Based on your OS, programs and softwares can be present in multiple directories. In general, the binary and the executable scripts are present in /bin
, /usr/bin
, /sbin
, /usr/sbin
directories, or somewhere under /opt
. They can also appear in /usr/local/bin
and /usr/local/sbin
, or in a directory in a user's account space, such as /home/student/bin
.
To locate your programs, you can use the whereis
command. If you have python3 installed in your linux distro, you can try writing whereis python3
Creating and Accessing directories
pwd
: Displays your current directorymkdir dirname
: Makes a new directory with name “dirname”cd dirname
: Change your current directory to dirnamecd
: Changes to your home directory
cd ..
: Change to the parent directorycd -
: Change to previous directoryls
: Lists the contents of that directory
Hard links
Hard links are like another name for an existing file. When you create a hard link, both the original file and the created hard link point to the same inode. If the original file is removed, the hard link still can access the data.
Let’s create a file name file1 and create a hard link for file 1 named file2
# creates a file
touch file1
# creates a hard link
ln file1 file2
Now, if you want to see the 2 files using the ls -li
tag, you’ll be able to see the inode number of the file in the 1st column.
You can clearly see that the inode numeber is same for file1 and file2
Soft links
Unlike hard links, soft link is like a shortcut to an existing file. If the original file is removed, the soft link is broken as it points to nothing
To create a softlink, use ln -s
command:
Now, after creating a soft link to file1 you can clearly see that the inode is different
Working with files
Viewing files
Command | Usage |
cat | Used for viewing files that are not very long; it does not provide any scroll-back. |
tac | Used to look at a file backwards, starting with the last line. |
less | Used to view larger files because it is a paging program. It pauses at each screen full of text, provides scroll-back capabilities, and lets you search and navigate within the file. |
NOTE*: Use* / to search for a pattern in the forward direction and ? for a pattern in the backward direction. An older program named more is still used, but has fewer capabilities: "less is more". | | tail | Used to print the last 10 lines of a file by default. You can change the number of lines by doing -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default. | | head | The opposite of tail; by default, it prints the first 10 lines of a file. |
Creating files
Use the touch <filename1> <filename2>
command to create multiple files in your current directory. To create directories, use the mkdir
command
Deleting files and folders
To remove a particular file, use rm <filename>
and to remove a folder use rmdir <dirname>
. Now, if you want to remove a directory with embedded directories and files which are not empty, then you’ll have to use rm -rf <dirname>
command
# remove file
rm file1
# remove directory
rmdir dir1
# remove non empty directory with files and embeded directories
rm -rf dir1
Moving, renaming
The mv
command is used to rename a file. Fundamentally if you see, moving a file to another directory is same as renaming the file. Do it practically
# create two folders test1 and test2
mkdir test1 test2
# cd to test1
cd test1
# create a file name hello.txt
touch hello.txt
# cd to the root
cd ../
# see the contents of test1
ls test1/
# now rename the file hello.txt to world.txt
cd test1
mv hello.txt world.txt
# see the contents of test1 now
ls
# cd to the root dir
cd ../
# see contents of test2
ls test2/
# now, we'll move the world.txt file to test2
mv test1/world.txt test2/world.txt
# now check the contents of test1 and test2
ls test1
ls test2
# rename the dirname test2 to test3
mv test2/ test3/
# check again
ls test3
Through this practical exercise, you’ll yourself be able to understand about the mv
command. Help yourself by not being lazy and try this
I forgot commands!
As you can see, there are multiple commands in linux. For file handling, for file searching, for copying files, removing files. There are a lot of commands and it is not possible to inspect through every one of them
Using the man command
Smart folks at linux got you covered. If you want to inspect the info about a command, you can use the man
command. For example, if I want to know about a command named adduser
, I can just type out man adduser
and it will show me detailed manual of that command
Here’s what happens when I type man adduser
in my terminal. Now, you can do that too, you can inspect through the document of any command and use your arrow keys to read about it
Using the apropos command
But you might think, what happens when we don’t know a command and want to search for it. Let’s say you want to search about which command will help you compress your file. What will your do then?
Well well, just use the apropos
command. In our case, to search commands about compression, we’ll just have to type out apropos compress
. Yes, it’s that simple
You will get the list of all commands which have the word “compress” in their manual
Conclusion
So, today let’s leave it here itself. Try out some commands in linux and make sure you don’t forget the man
and apropos
command as it will act as your in your journey to explore linux. We’ll dive deeper into the linux filesystem in the next article, till then keep learning and thanks
Subscribe to my newsletter
Read articles from Snehendu Roy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
