Switching to Linux. Command line

This is the fourth article in our series on migrating to Linux. If you missed the previous articles, we covered them in Linux for new users, files and filesystems, and graphical environments. Linux is everywhere. It is used to run most of the internet services such as web servers, email servers, and others. It is also used in your mobile phone, your console, and more. As such, you might be interested in trying Linux and learning more about how it works.

On Linux, the command line is very useful. On Linux desktops, while the command line is optional, you’ll often see people have a command prompt window open along with other application windows. On internet servers, and when Linux is running on a device, the command line is often the only way to directly interact with the system. So, it’s good to know at least some command line basics.

On the command line (often referred to as a shell in Linux), everything is done by typing commands. You can list files, move files, display file contents, edit files, and more, even display web pages, all from the command line.

Navigation

The command line has the concept of the current working directory (Note: folder and directory are synonyms, and in Linux they are usually called directories). Many commands will appear in this directory by default unless a different directory path is specified. For example, typing ls in the file list will list the files in that working directory. For instance:

$ ls

Desktop Documents Downloads Music Pictures README.txt Videos

The ls Documents command will instead be a list of files in the Documents directory:

$ ls Documents

report.txt todo.txt AndreyEx.pdf

You can display the current working directory by typing pwd. For instance:

$ pwd

/home/student

You can change the current directory by typing cd followed by the directory you want to change to. For instance:

$ pwd

/home/student

$ cd Downloads

$ pwd

/home/student/Downloads

A directory path is a list of directories separated by a / (forward slash) character. The directories in the path have an assumed hierarchy, for example, if the path is / home / student, it expects a directory named home in the top directory, and a directory named student in that directory home.

Directory paths are absolute or relative. Absolute directory paths start with a /.

Relative paths start with. (point) or .. (point point). On the way, a. (dot) means the current directory and .. (dot dot) means one directory up from the current directory. For example, ls ../Documents means browsing in a directory to one of the current one and displaying the contents of a directory named Documents:

$ pwd

/home/student

$ ls

Desktop Documents Downloads Music Pictures README.txt Videos

$ cd Downloads

$ pwd

/home/student/Downloads

$ ls ../Documents

report.txt todo.txt AndreyEx.pdf

When you first open a command prompt window on a Linux system, your current working directory is set to your home directory, typically: / home / . Your home directory is for login where you can store your own files.

The $ HOME environment variable expands to a directory path to your home directory. For instance:

$ echo $HOME

/home/student

Search

Sometimes we forget where the file is, or we forget the name of the file we are looking for. There are several commands on the Linux command line that you can use to help you find files and search for the contents of files.

The first command is find. You can use find to find files and directories by name or some other attribute. For example, if we forgot where I saved the todo.txt file, we can run the following:

$ find $HOME -name todo.txt

/home/student/Documents/todo.txt

The find program has many functions and capabilities. Simple command form:

find <dкаталог для поиска> -name <имя файла>

If the above example has more than one file named todo.txt, it will show us all the locations where it found a file by that name. The find command has many options for searching by type (file, directory, or other), date, newer than date, size, etc. You can enter:

man find

for help on how to use the find command.

You can also use grep command to search inside files for specific content. For instance:

grep "09/02/2018" todo.txt

will show me all the lines that have the date September 2, 2018.

Get help

There are many commands in Linux, and it would be too difficult to describe all of them here. So, the next best step is to show you how to get help by command.

The apropos command helps you find commands that perform specific actions. Perhaps you want to know all the commands that work with directories or get a list of open files, but you don’t know which command to run. So, you can try:

apropos directory

will provide a list of commands and contain the word “directory” in the help text. Or you can do:

apropos "список открытых файлов"

which will show one lsof command that you can use to list open files.

If you know the command you need to use, but don’t know what options to use to make it behave the way you want it, you can use the man command, which is inappropriate for a manual. You would use man like:

man ls

You can try man ls yourself. He will provide several pages of information.

The man command explains all the options and options you can give the command, and often even gives an example.

Many commands often also have a help option (for example, ls –help) that will give information on how to use the command. The man pages are usually more detailed, and the –help option is useful for quick searches.

Scripts

One of the best things about the Linux command line is that the commands you type can be scripted and run over and over again. Commands can be placed as separate lines in a file. You can put #! / Bin / sh as the first line in the file, followed by commands. Then, once the file is marked executable, you can run the script as if it were its own command. For instance,

--- contents of get_todays_todos.sh ---

#!/bin/sh

todays_date=`date +"%m/%d/%y"`

grep $todays_date $HOME/todos.txt

Scripts help automate certain tasks in a set of repeatable steps. Scripting can also be very complex if needed with loops, conditionals, subroutines, etc. There is no room for details here, but you can find more information on Linux bash scripts on the Internet.

Familiar with the Windows Command Prompt?

If you are familiar with the Windows CMD or PowerShell program, you should be familiar with the command line input commands. However, in Linux, several things work differently, and if you don’t understand these differences, it can be confusing.

First, the PATH environment variable works differently on Linux than it does on Windows. On Windows, the current directory is considered the first directory in the path, although it is not listed in the PATH. Under Linux, the current directory is not considered to be in the path, and it clearly does not fit in the path. Typing in the PATH environment variable is considered a security risk in Linux. On Linux, to run a program in the current directory, you need to prefix it with ./ (this is the relative path of the file from the current directory). This is causing a lot of CMD users. For instance:

./my_program

more likely than

my_program

In addition, on Windows, paths are separated by a; (semicolon) in your PATH environment variable. On Linux, in PATH, directories are separated by: (colon). Also on Linux, directories in the same path are separated by a / (backslash) character, while under Windows directories along the same path are separated by a (backslash) character. Thus, a typical Windows PATH environment variable might look like this:

PATH="C:Program Files;C:Program FilesFirefox;"
while on Linux it might look like:
PATH="/usr/bin:/opt/mozilla/firefox"

Also note that environment variables are expanded with $ on Linux, so $ PATH expands to the contents of the PATH environment variable, whereas on Windows, you need to enclose the variable in percentage characters (for example,% PATH%).

On Linux, parameters are usually passed to programs that use the – (dash) character in front of the option, while under Windows options, the previous parameters are passed with the / (forward slash) character. So, on Linux, you would do:

a_prog -h

more likely than

a_prog /h

In Linux, file extensions usually mean nothing. For example renaming myscript to myscript.bat does not make it executable. Instead of making it executable, you need to set the executable permission flag. Next time, the file permissions will be discussed in more detail.

On Linux, when file and directory names start with. (dot) they are hidden. So, for example, if you are prompted to edit a file, .bashrc, and you do not see it in your home directory, this is probably the case. It’s just hidden. On the command line, you can use the -a option to the ls command to view hidden files. For instance:

ls -a

In Linux, common commands are also different from normal Windows command line commands. The following table shows the mapping of common elements used in CMD and the alternative used in Linux.

Please disable your ad blocker or whitelist this site!

Sidebar