2.11.17. Useful features of the console
There are many useful features in the console, such as keyboard shortcuts or generic arguments, which are used to make commands more flexible.
Bash shell customization
There are two files in the Bash shell that allow you to customize the shell used in the CLI to suit your needs. For example, create certain aliases when connecting or set the necessary parameters, which is very useful for frequent work. Files:
.bash_profile
— file processed automatically when connected to the console. Since usually all created shell parameters are placed in.bashrc
, then a file call should be placed in this file.bashrc
with code like this:if [ -f ~/.bashrc ]; then . ~/.bashrc fi
.bashrc
— file processed for unauthorized connections, for example, when connecting via WebSSH... Most often, this file is called from the side.bash_profile
to create the same experience when connecting with or without authorization. The file contains all the parameters and commands that need to be executed after connecting.
Hotkeys
- Tab — auto—completion, usually used to auto—complete the name of a directory or file, if there are several files with a similar name, then double—clicking will display a list of suitable files.
- Ctrl+R — interactive search by previously executed commands.
- Ctrl+Alt — move to the beginning of the team.
- Ctrl+E — move to the end of the command.
- Ctrl+D — exit the current connection / command console.
Useful commands
pwd
— displaying the path to the active directory.cd
— changing the active directory.ls
— displaying a list of files and directories in the active directory or at the specified path.ll
— the same asls -l
, output files and directories as a readable list.cat
— output the contents of the file.head
— output the first few lines of the file content.tail
— output the last few lines of file content, output new lines in real time when completing the file.less
— a page—by—page view of a file or command output. To exit you need to press Q.touch
— updating the date of the last file modification to the current one. Also creates a file if nonexistent is specified as a parameter.mkdir
— creating a directory.rm
— deleting a file or directory.grep
— a tool for searching files and directories by content. Described in this instructions.find
— a tool for searching files and directories. Described in this instructions.diff
— comparison of two files, the command output will only show the difference between the files.man
— get information about the team and its capabilities.xargs
— using the output of the previous command as an argument for the next one. Application:command1 | xargs command2 stdout_command1
Arguments
~/
— indication of the user's directory, on the hosting it is/home/user/
whereuser
thisis hosting account name.>
— put the command output into a file, replacing all its contents. Redirecting output has more options than writing data to a file, but these are not covered here.>>
— put the command output into a file, appending it at the end. Redirecting output has more options than writing data to a file, but these are not covered here.!!
— substitution of the previous command.!*
— substitution of the key of the previous command.!$
— substitution of the last argument of the previous command.!abc
— substitution of a previously executed command that begins with abc.!?abc?
— substitution of the last previously executed command containing the text abc.example.file{,.bak}
— reduction of two lines to one if the second line uses the same text as the first, but with an ending.bak
... For example, this is useful when creating a temporary copy of a file.cp config.php{,.bak}
.
Sequences
;
— execute a series of commands, separated by this symbol, sequentially, while the execution of the next command does not depend on the result of the previous one.&&
— execute a series of commands, separated by this symbol, sequentially, but unlike the previous version, the next command will be executed only if the previous command did not return an error. The symbol is similar to the logical condition «AND (AND)».||
— execute a series of commands separated by this symbol, but the next command will be executed only if the previous command returned an error during execution. The symbol is similar to the logical condition «OR (OR)».
Examplesof
Deleting files containing a specific line
Attention!
Before executing, make sure that the command is specified correctly and create a backup of your entire account, as deleting files may become irrecoverable.Example
— search text in filesfolder
— the target directory within which the search will be performed.
grep -lr 'Example' ~/folder | xargs rm -f --