Cheatsheet (Comand Linux)
The type command identifies the command for example “cd”, as an internal command.

type cd
For external commands, the type
command displays the location of the command:

type cal
Using the -a option of the type command displays all locations that contain the command named:

type -a echo
The which command searches for the location of a command by searching the PATH variable.

which ls
which cal
alias name=command

fecha
alias=fecha"date"
fecha
function_name ()
{
commands
}

prueba_funcion () {
> ls -l /home
> echo "fin"
> }
prueba_funcion
Diferència utilitzar “;” o utilitzar “|”
El primer no trepitja la informació mostrada al terminal i el segon si, encara que el segon encadena les comandes per mostrar un resultat final.

cal 1 2030 | cal 2 2030
cal 1 2030 ; cal 2 2030 ; cal 3 2030
The double ampersand && acts as a logical “and”; if the first command is successful, then the second command will also run. If the first command fails, then the second command will not run.

ls /etc/ppp/ && echo fin
The double pipe ||
is a logical “or”. Depending on the result of the first command, the second command will either run or be skipped.

ls /etc/ppp/ || echo ha fallado comando ls
sl /etc/ppp/ || echo ha fallado comando ls
man is the system’s manual pager. Each page argument given to man is
normally the name of a program, utility or function. The manual page
associated with each of these arguments is then found and displayed. A
section, if provided, will direct man to look only in that section of
the manual. The default action is to search in all of the available
sections following a pre-defined order (see DEFAULTS), and to show only
the first page found, even if page exists in several sections.

man man
The whatis “command” (or man -f) returns what section a man page is stored in.

whatis ls
To search for the location of a command or the man pages for a command, use the whereis command. This command searches for commands, source files and man pages in specific locations where these files are typically stored.

whereis ls
To find any file or directory, use the locate command. This command searches a database of all files and directories that were on the system when the database was created. Typically, the command to generate this database is run nightly.

locate gshadow
To display the info documentation for a command, use the info command.
info “command”

info ls
Note that to close the help screen type the L key, which brings back the current document. To quit entirely, use the Q key.

info
Instead of using info documentation to look up information about a specific command or feature, consider exploring the capabilities of Linux by reading through the info documentation. Execute the info command without any arguments to be taken to the top level of the documentation.

info
Many commands will provide basic information, very similar to the SYNOPSIS found in man pages, by simply using the –help option to the command. This option is useful to learn the basic usage of a command quickly without leaving the command line:

cat --help
These documentation files are often called readme files since the files typically have names such as README or readme.txt. The location of these files can vary depending on the distribution that you are using. Typical locations include /usr/share/doc and /usr/doc.

ls /usr/share/doc
To determine where the user is currently located within the filesystem, the pwd (print working directory) command can be used:

pwd
The asterisk * character is used to represent zero or more of any character in a filename. For example, to display all of the files in the /etc directory that begin with the letter t:

echo /etc/s*
Suppose you want to display all of the files in the /etc directory that begin with the letter t and have exactly 7 characters after the t character:

echo /etc/t???????
The bracket [] characters are used to match a single character by representing a range of characters that are possible match characters. For example, the /etc/[gu]* pattern matches any file that begins with either a g or u character and contains zero or more additional characters:

echo /etc/[gu]*
The -v option causes the cp command to produce output if successful. The -v option stands for verbose:

cp -v /etc/hosts
touch archive modified date of the archive

ls -l Firefox_wallpaper.png
Creating an archive with the tar command requires two named options:

tar -cf imagencomprimida Firefox_wallpaper.png
ls -l Firefox_wallpaper.png
ls -l imagencomprimida
There are many movement commands for the less command, each with multiple possible keys or key combinations. While this may seem intimidating, it is not necessary to memorize all of these movement commands. When viewing a file with the less command, use the H key or Shift+H to display a help screen:

The head and tail commands are used to display only the first few or last few lines of a file, respectively (or, when used with a pipe, the output of a previous command). By default, the head and tail commands display ten lines of the file that is provided as an argument.

head /etc/sysctl.conf
Passing a number as an option will cause both the head and tail commands to output the specified number of lines, instead of the standard ten. For example to display the last five lines of the /etc/sysctl.conf file use the -5 option:

tail -5 /etc/sysctl.conf
Upon close examination of the output in the preceding example, the sort command has arranged the lines of the file in alphabetical order. Compare this output to the output of the previous cat command.

sort /etc/sysctl.conf
The wc command provides the number of lines, words and bytes (1 byte = 1 character in a text file) for a file, and a total line count if more than one file is specified. By default, the wc command allows for up to three statistics to be printed for each file provided, as well as the total of these statistics if more than one filename is provided:

wc /etc/sysctl.conf
The cut
command can extract columns of text from a file or standard input. It’s primarily used for working with delimited database files. Again, delimited files are files that contain columns separated by a delimiter. These files are very common on Linux systems.

cut -d: -f1, 5-7 mypasswd
The grep command can be used to filter lines in a file or the output of another command that matches a specified pattern. That pattern can be as simple as the exact text that you want to match or it can be much more advanced through the use of regular expressions.

grep bash /etc/passwd
One of the most useful expressions is the period .
character. It matches any character except for the new line character. Consider the unfiltered contents of the ~/Documents/red.txt
file:

grep 'r..f' red.txt