Friday, July 2, 2021

Linux Console Tutorial

This document is intended to provide a foundation of skills for using Linux in console or shell mode. The reader is not expected to have any prior knowledge of how Linux works, although in this case the reader should at least look through Linux Concepts and Running Commands before moving on to the Command Reference. The Command Reference introduces the core set of Linux commands categorized by the type of functionality they provide. The final section on Advanced Features illustrates several techniques that will help you get the most out of Linux. 

  

Linux Concepts

Users:

In order to use a Linux machine you must have an account on that machine. The account information specifies your user name, and password, which together control your access to the machine. There is also a unique integer associated with your user account called your user ID or UID. 

Groups:

Users working for the same department or on a joint project can be put into a named group. This is simply a method of organizing users. Like with individual users, there is a group id or GID associated with each group. 

Files:

All information stored on a Linux machine is stored in files. In addition to containing user or program data a file has information associated with it regarding which user owns it, which users can access it, when it was created, when it was last modified, and how large it is. 

Directories:

Directories are like folders. They can contain files as well as other directories, and give structure to the file system. Under Linux there is only one directory tree and it's root is /. Like files, each directory has associated with it information as to it's size and which users are allowed to access it, etc... 

Paths:

A path is a string of directories possibly ending with a file name. The directory and file names are separated by / (front slash) characters. An example is: /dir1/dir2/file which is a path to file contained in dir2, which is in turn contained in dir1, which is contained in the root directory, /. 

Permissions:

Permissions are an important feature of Linux. They provide security by limiting what actions a user can perform on a given file or directory. Permissions control read, write, and execute access to a file for the file's owner, group and other users. If you try to manipulate a file in a manner for which you do not have sufficient privileges, an error occurs. 

Processes:

When a user runs a command Linux creates a process to contain the command's instructions (code). A process also contains control information such as the UID of the user who started the command, and a unique process id or PID. All management of processes is done using this unique PID. 

The Shell:

In console mode a user interacts with the machine through a shell. A shell is a program used to start other programs. A shell is configured by setting it's environment (configuration) variables. When you log on to a Linux machine it will automatically create a shell process for you, and set the shell's environment variables to default values. Note that this document assumes the reader is using bash (the Bourne Again SHell), which is the default for most current linux distributions. 

  

Running Commands

Entering Commands:

To enter a command you simply type it's name after the shell prompt and press enter. The shell prompt is usually of the form [user@host directory]$, but is configurable, and can vary from machine to machine. Most commands usually accept several arguments or options (sometimes called flags). Normally arguments are set off using one or two -'s (dashes). If a command requires arguments and none are supplied, the command will usually display a brief usage message describing the arguments it requires. A typical command with arguments might look like:


command -a1 -a2 

command --long_argument_name 

The PATH:

The PATH is a shell environment variable that determines which directories Linux will look in for a command's program file if a full path to that file is not specified. The PATH variable consists of a string of directory names separated by :'s (colons). Most if not all of the commands listed in the reference reside in directories that are specified in the PATH and can be run simply by entering their names at the prompt. For security reasons the current directory is not in the PATH so to run a program contained in the current directory you must prefix ./ (dot front slash) to the programs name as in:


./command 

  

Command Reference

Getting Help:

Most console Linux commands contain small routines that print out usage information when a -h or --help flag is passed to them. In addition, most Linux machines have literally thousands of pages of documentation stored on them. One of the most convenient sources of information is the man page system. You need only supply the man program with the name of a command as an argument and it will retrieve and display the complete manual for that command. 


 command -h: Display a brief help message about command.


 command --help: Display a brief help message about command.


 man command: Show the complete manual page for command. 


Listing Files:

One of the most basic operations you can perform is to list the files in a directory with ls. This lets you examine the contents of the directory and find the files you want to work with. If the directory listing is too long to fit on one screen you may want to pipe the out put of ls to a text viewing program like less (see section 4.6). 


 ls: Lists the content of the current directory. 


 ls -a: List all files including hidden files.


 ls -l: Use long listing format (displays permissions, owner, size, date, etc...).


 ls | less: Pipe the directory listing into less for easy viewing. 

Changing Directories:

When you log on to a linux machine you are automatically placed into your home directory. To change to another directory you use the cd command. cd accepts a full path to a directory, a path relative to the current directory, or one of a number of special arguments displayed below. 


 cd path: Change to the directory specified by path.


 cd ~ : change to your home directory.


 cd - : change to your previous directory.


 cd .. : change to the current directory's parent.

Managing Files and Directories

The cp command is useful for making a backup copy of a file or a directory. The mkdir command creates a new, empty directory at the specified location. mv can be used to move a file from one directory or another, or to change it's name by moving it to a different file name in the same directory. The rm lets you delete old files and free up storage space on the system.You can use rm -R to recursively delete the contents of a directory, and then delete the directory itself. rmdir will remove a directory, but only if it is already open. 


 cp source_path destination_path: Copy files from source to destination.


 cp -R source_path destination_path: Recursive copy. 


 mkdir directory: Make a new directory. 

 mv source_path destination_path: Move or rename files. 

 rm files: Remove files. 

 rm -R directory: Recursive delete. 

 rmdir directory: Remove an empty directory. 

 touch file: Create an empty file. 

Locating Files

When your files are spread over many directories, or you are looking for a specific system file, find and locate can save you manually searching directories. find will start from the specified directory and search the whole subtree from that point on. locate, on the other hand, maintains a database of all the files on the system. It simply looks in the database to see if any files match the supplied name. locate is much faster than find, but it's database is only updated once a day so it will not be able to find recently created files. 


 find path -name filename: Find the file called filename starting the search from path. 

 locate filename Find files in the database whose names contain filename. 

Working with Text Files

To view the contents of a short text file use cat to print it to the screen. less allows you to scroll through a long text file using the regular arrow keys as well as the page up, and page down keys. grep is a tool for searching text files for a specific string. When grep finds an instance of the string it will print the whole line it occurs on. sort, as the name implies, will sort a text file and display the results on the screen. Lastly, pico is a small text editor with built in search and spell check features. 


 cat filename: Print the contents of a text file called filename to the screen. 

 grep string filename: Search for and display lines of filename that contain occurrences of string. 

 less filename: Scroll through the content of a text file. Press q to quit. 

 pico filename: Open filename in a simple text editor. 

 sort filename: Sort the lines in filename by alphabetical order. 

Uncompressing Archives

When retrieving files from the Internet they are usually compressed in some way to save space. Most often a large number of files will be compressed into a single archive file for easier downloading. To get at the files in the archive you need to determine how the archive was created. Normally the file extension of the archive will indicate which tool is required to uncompress it. 


 bunzip2 filename.bz2: Uncompress a bzip2 file (*.bz2). Used for big files. 

 gunzip filename.gz: Uncompress a gzipped (.gz) archive. 

 unzip filename.zip: Uncompress a PkZip or WinZip file (*.zip) 

 tar -xvf filename.tar: Untar a tar (.tar) archive (tarball). 

 tar -xvzf filename.tar.gz: Uncompress and untar a compressed tar archive (*.tar.gz or *.tgz). 

Using Network Services

Network services allow you to access resources on other machines (servers) in the network from your machine. The ftp program allows you to connect to an FTP (file transfer protocol) server. An FTP server will let you upload (put) an download (get) files. FTP is not a secure protocol and all communications to the FTP server are sent as plain text, including your password. The ping command will issue a ping request to the specified server. Most machines will answer a ping request if they are connected to the network and working correctly. The ssh program will let you connect to a server running the Secure Shell protocol. Using ssh you can log on to another machine and use it in console mode from your own machine. SSH is a secure protocol so all communications with the remote server are encrypted. telnet serves the same purpose as ssh, but is an older, unsecured protocol. Some older machines only run TELNET servers, but whenever possible ssh should be used instead. 


 ftp server: Connect to an FTP server. 

 ping server: Send a ping request to server. 

 ssh -l user server: Connect to a remote machine using the Secure Shell protocol. 

 telnet server: Connect to another machine using the TELNET protocol. 

Printing

Access to printing from the Linux console is provided by the Lpr (Line PRinter) set of commands. Using the lpr command you can print a text or PostScript (.ps) document to any attached printer. To see what printers are available on the system you can use the lpstat command. The lpq command lets you examine the contents of the print queue for a given printer. The display shows the numeric job ID, owner, file name of each job in the queue. Lastly, the lprm command will remove a job from a print queue by it's job ID. Of course, you may only remove a job from a queue if you placed it there initially. 


 lpq -Pprinter: Show jobs in the print queue for printer. 

 lpr -Pprinter document: Print document to printer. 

 lprm -Pprinter n: Remove job with ID n from printer. 

 lpstat -a: Show status information for all available printers. 

Viewing System Information

The following commands display miscellaneous information about the system. 


 date: Print the operating system time and date. 

 df -h: Print disk usage information. 

 free: Print memory usage information. 

 history: Show commands executed on the current account. 

 hostname: Print the name of the local machine (host). 

 pwd: Print working (current) directory. 

 rwho -a: List all users logged into the network. 

 uptime: Print the amount of time since the last reboot. 

 who: List the users logged into the machine. 

 whoami: Print your login name. 

Controlling Processes


 ps: List the active processes you own and their PID's. 

 ps -aux: List all the active processes, together with the name of the user that owns each process. 

 top: Show a continually updated listing of active processes. 

 command &: Run command in the background. 

 fg: Bring a background or stopped process to the foreground. 

 bg: Send the process to the background. The same can be accomplished with Ctrl-z. 

 kill pid: Force a process to shutdown. First determine the pid of the process to kill using ps. 

 killall -9 name: Kill all processes with the specified name. 

 nice program level: Run program with niceness level. Specifying a high nice level will make the program run with a lower priority. 

Changing File Permissions


 chown user.group filename: Change the user which owns filename to user, and the group which owns filename to group. Of course, you can only chown a file you already own. 

 chmod [augo][+-][rwx] filename: Modify the access permissions on filename by granting or revoking (+-) read, write, or execute (rwx) access to all, owner, group, or other users (augo). Use with care. You can deny yourself access to your own files by setting incorrect permissions on them. 

  

Advanced Features

Redirection:

The shell gives all programs access to three pointers called standard input (STDIN), standard output (STDOUT), and standard error (STDERR) when the program is started. Normally STDIN is connected to the keyboard, while STDOUT and STDERR are connected to the screen. However, with redirection you can change where a program gets it's input from, and where it sends it's output to. The redirection operator > lets you specify a file name to receive the STDOUT (output) of the command, while the < operator lets you connect STDIN to a file containing input. Some examples are:


command1 > output.txt send output to output.txt

command1 < input.txt get input from input.txt

Piping:

The main purpose of piping is to redirect STDOUT from one process to STDIN of another process. The pipe operator is |, the pipe character (found above the enter key on most keyboards). To pipe the output of command1 to the input of command2 you would enter the following: 


command1 | command2


https://www.fosslinux.com/45587/linux-command-cheat-sheet.htm

No comments:

Post a Comment