I still remember my first encounter with the Linux command line interface, I was learning how to compile a “helloworld.c”
program file with the gcc compiler. I knew very little about the Linux system or environment itself at the time. Someone leaving the Windows environment, where everything is done seamlessly with a click ,might get lost or find it difficult when moving to a Linux system. Sometimes a click is not a enough to get a task done on Linux hands need to get real dirty. Mastering the command line on Linux makes you a demi god. Hellas the author himself is still a humble servant to the …
This article is made to help any one get started with command line on Linux. In this article we shall be working on the Ubuntu distribution of Linux and the gnome environment. A series of articles will be published to help new users love command line.
To access the terminal just press the combination of this three keys on your keyboard.
Ctrl + Alt + T
Or you click on the applications bar
Search for terminal and you will see this interface below.
Click on terminal and you should see the terminal appear.
Using the Linux command line is all about calling utilities. These utilities are small programs/scripts that perform particular/specialized tasks such as creating a file, deleting files, searching a file, opening a program, download files, record desktop, installing other utilities or programs just to name a few. These utilities have a basic structure or form.
<utility_name> [flags, arguments ]
Flags may act like sub operations to be carried out by the utility. The give additional operations added to the default behavior of the utility. Example type in you command line
sudo ls
ls command line flags is the name of the utility . This will list files in the current directory. We can add the flags -al this will list all files in the current directory including hidden files (files whose names start with a period) and also show the various user,group and other permissions of each file.
ls -al
Instead of the current directory we are in, we can list the files found in a particular directory. For example /home/pictures
. Here the path will be an argument to the utility.
ls -al /home/pictures
We might want to use a utility and we do not know what its flags are ,options or form of its arguments. There are utilities like man and info that gives information about the usage of a utility/command
. man gives a documentation of the program and info gives a deeper and richer explanation of the command and its usage. In your terminal type the following commands and notice the difference
man ls
info ls
Type the following commands to practice. First start by resetting your command line interface by typing
reset
Get the path of your working directory
pwd
List all the files in your working directory
ls -a
Creating directories using the mkdir command, use sudo in front of the command if your face permissions issues. We are going to create a directory hello_world_command. After creating the directory ,run ls and verify it was created.
sudo mkdir hello_world_command
Enter into the hello_world_command
directory by using the command cd(change directory) . Note the period . in the path, it’s a reference to the current/working directory, we can omit it and put just hello_world_command
instead of ./hello_world_command
. cd will look implicitly look for hello_world_command directory in the working directory.
cd ./hello_world_command
Notice the path has changed . You can type pwd to get the current path. Create another directory hello in hello_world_command
directory.
sudo mkdir hello && cd hello
&& is used to chain operations, in this case the directory hello is first created and we later enter into the created hello directory. The command touch is used for creating files. Create a text file called hello_world.txt
in the directory hello.
sudo touch hello_world.txt
Writing text to a file can be done using the echo command. The command echo prints its output by default on the stdout (standard output) that is the terminal console, we can change the destination source by using >>
echo "Add this text to hello_world.txt file" >> hello_world.txt
Use the command cat to print the content of a file.
cat hello_world.txt
Create another directory, hello_world_2
cd ../../ && mkdir hello_world_2
..
(two dots) is a reference for the parent directory, doing a cd ../
moves us up one directory higher, basically what was done above, was moving two directories higher and creating a directory at the same level with hello_world_command
.
Copying files is done with the cp command, copy hello_world.txt
from hello directory to hello_world_2
directory.
cd hello_world_2
cp ../hello_world_command/hello/hello_world.txt ./
Renaming and moving(cut) files is done with the same command mv Rename hello_world.txt
to renamed_hello_world.txt
mv hello_world.txt renamed_hello_world.txt
Moving renamed_hello_world.txt
from hello_world_2
directory to hello_world_command
and verify the file was moved to that directory.
mv ./renamed_hello_world.txt ../hello_world_command/
cd ../hello_world_command && ls
The command for deleting files and directories is rm . Deleting can be done interactively , -i flag,that is you will be asked if you really want to delete the file and you can choose either y(yes) or n(no). Or deleting can be done without prompting user to confirm ,-f flag. When deleting a directory the recursive -r flag is needed. Be very careful using the r and f flag together, when running as root user these might lead to a complete erase of your computer if you do not know what you are doing.
rm -i renamed_hello_world.txt
Deleting a directory
rm -ir hello
Searching for file or directory the utility find is used. Create 2 files , test.txt and test2.md, and fill them with the text “Cats are lovely” and “Dogs are so friendly” respectively. Note you are still in the directory hello_world_command.
touch test.txt test2.md dump.pdf
echo "Cats are lovely" >> test.txt
echo "Dogs are so friendly" >> test2.md
cd .
Move out of the hello_world_command directory with the last cd command. Now search for a file with name test.txt, files that start with “test” and files with the extension “pdf” .When searching for directories instead ,replace fwith d .
find ./ -type f -name test.txt
find ./ -type f -name "test*"
find ./ -type f -name *.pdf
Searching for files containing a particular text can be done by combining two commands find and grep. Search for a file containing the substring “Dogs” .
find ./ -type f -exec grep -i 'Dogs' {} \;
We are done for this one, how you feeling , sense of a hacker right? In our next article we will talk about installing programs using the command line.
See you then.
By receiving free stock articles and smart tutorials to advance your career...