2.11.16. Useful console capabilities

The console offers many useful features, such as hotkeys or universal arguments, which are used for more flexible command application.

In the Bash shell, there are two files that allow you to configure the CLI shell according to your needs. For example, you can create specific aliases upon connection or set the necessary parameters, which is very useful for frequent work. The files:

  • .bash_profile — the file that is automatically processed when connecting to the console. Since all created shell parameters are usually placed in .bashrc, this file should include a call to the .bashrc file using the following code:
    if [ -f ~/.bashrc ]; then
    . ~/.bashrc
    fi
  • .bashrc — a file processed for unauthorized connections, for example, when connecting via WebSSH. Most often, this file is called from .bash_profile to create the same working conditions when connecting with or without authorization. The file contains all the parameters and commands that need to be executed after connection.
  • Tab — autocomplete, typically used for completing the name of a directory or file. If there are multiple files with similar names, double pressing will display a list of matching files.
  • Ctrl+R — interactive search through previously executed commands.
  • Ctrl+Alt — move to the beginning of the command.
  • Ctrl+E — move to the end of the command.
  • Ctrl+D — exit the current connection/command console.

Navigation and working with directories:

  • pwd — output path to the active directory.
  • cd — change the active directory.
  • ls — output a list of files and directories in the active directory or along the specified path.
  • ll — the same as ls -l, displays files and directories in a human-readable list format.
  • mkdir — creating a directory.
  • rm — deletion of a file or directory.
  • touch — updates the last modification date of a file to the current date. Also creates a file if a non-existent one is specified as a parameter.

Viewing and working with files:

  • cat — output the contents of a file.
  • head — output of the first few lines of a file's content.
  • tail — outputs the last few lines of a file's content, displays new lines in real-time as the file is appended.
  • less — paginated viewing of a file or command output. To exit, press Q.
  • nano, vi, vim — text editors in the terminal.
  • file — defines the file type. Example: file index.php.
  • diff — comparison of two files, the command output will only show the differences between the files.

Data search and analysis:

  • grep — search for files and directories by content. For more details, see Search files and directories using console.
  • find — search for files and directories. For more details, see Search files and directories using console.
  • duanalysis of disk space, which is used by files and directories. Examples: du -sh * or du -h –max-depth=1.
  • wc — count the number of lines, words, and characters. Example: wc -l error.log.
  • sort — sorting lines of a file. Example: sort file.txt.
  • uniq — removing duplicate lines. Example:: sort file.txt | uniq -c.
  • cut — extraction of columns by delimiter. Example: cut -d":" -f1 ./.htpasswd.
  • awk — line-by-line processing of text data. Example: awk '{print $1, $3}' file.txt.
  • sed — a stream editor for text replacement, often used in conjunction with awk. Example: sed 's/error/warning/g' file.log.
  • ncdu — utility for analyzing disk space and managing files and directories in interactive mode.

System information and processes:

  • ps — displays a list of the user's active processes. An example of outputting detailed information of all running processes and filtering by the substring php: ps aux | grep php.
  • strace — tracing of a program's system calls, which is useful for analysis and debugging. Example: strace -e openat,connect ./script.sh — trace file and network accesses.
  • env — output of all environment variables.
  • which — output path to the executable file. Example: which php.
  • history — displays the history of executed commands.
  • date — current date and time. Example: date "+%d-%m-%Y %H:%M:%S".
  • man — output of information about the command and its features.
  • xargs — using the output of the previous command as an argument for the next one. Example: command1 | xargs command2.

Archiving and compression:

File transfer:

  • scp — copying files and directories between a local and remote server via a secure SSH connection. Example: scp file.txt user@host:/path/.
  • rsync — synchronization of files and directories between a local and remote server. Example: rsync -avz ./public_html/ backup@remote:/backup/. See also Create and restore copy of site files using rsync.

Working with the network:

  • curl — sending HTTP requests and checking URL availability. Example: curl -I https://example.com.
  • wget — downloading files via URL, has far fewer features compared to curl.

Working with CMS:

  • wp — utility for working with WordPress sites. For more information, see WP-CLI utility.
  • drushseparately installed utility for managing Drupal-based sites.
  • composer — console dependency manager for PHP. For more information, see Work with Composer.
  • artisan — console interface for working with the Laravel framework, which comes bundled with it. For more information, see Artisan utility.
  • ~/ — means the current user's directory, on the hosting server this is /home/user/, where user is the hosting account name.
  • > — saving the command output to a file, replacing the current file contents. Redirecting output has more capabilities than writing data to a file; detailed information about them can be found on the web.
  • >> — saving the command output to a file in addition to the current file contents. Redirecting output has more capabilities than writing data to a file; detailed information about them can be found on the web.
  • !! — 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} — reducing two lines to one, if the second line uses the same text as the first, but with the ending .bak. For example, this is useful when creating a temporary copy of the file cp config.php{,.bak}.
  • ; — commands separated by this character are executed sequentially; the next command is executed regardless of the result of the previous one.
  • && — commands separated by this character are executed sequentially, but the next command will only be executed if the previous command was executed without errors. The character is similar to the logical condition "AND".
  • || — commands separated by this character are executed sequentially, but the next command will only be executed if the previous command ended with an error. The character is similar to the logical condition "OR".

Attention!

Before executing potentially dangerous commands, create a backup of your entire hosting account so that you can restore your files if necessary.

Deleting files with specific text:

grep -lr 'Example' ~/folder | xargs rm -f --
  • Example — desired text in files.
  • folder — target directory in which the search will be performed.
Treść

    (1)

    Komentarze

    amelie
    Мождиво "вивід" а не "висновок".
    verliber
    Дякуємо за зауваження, виправили текст статті.