User Tools

Site Tools


linux:commands

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:commands [2017/08/23 07:51] torpedolinux:commands [2023/12/03 11:10] (current) – [Create link] tormec
Line 1: Line 1:
 +====== Basic commands ======
 +
 +
 +===== System informations =====
 +
 +To know name and version of one's Linux distribution:
 +<code bash>
 +cat /etc/lsb-release
 +</code>
 +
 +To get kernel (''Architecture'') and processor architecture (''CPU op-mode(s)''): <code bash>
 +lscpu
 +</code>
 +
 +To view free and used memory:
 +<code bash>
 +free
 +</code>
 +
 +To view free and used disk space:
 +<code bash>
 +df
 +</code>
 +
 +To view a detailed description of the system's hardware components:
 +<code bash>
 +sudo dmidecode
 +</code>
 +<WRAP info>
 +**Info:** to find out the maximum ram capacity of the computer, refer at ''type 16'' of ''dmidecode'' output, for instance:
 +<code>
 +Handle 0x000F, DMI type 16, 15 bytes
 +Physical Memory Array
 + Location: System Board Or Motherboard
 + Use: System Memory
 + Error Correction Type: None
 + Maximum Capacity: 4 GB
 + Error Information Handle: Not Provided
 + Number Of Devices: 2
 +</code>
 +where ''Number of Devices'' is the number of slots.
 +</WRAP>
 +
 +
 +===== Switch among directories =====
 +
 +To come back from ''dir/sub-dir'' to the parent directory ''dir'':
 +<code bash>
 +cd ..
 +</code>
 +
 +To go to any directory:
 +<code bash>
 +cd /absolute/path/folder
 +</code>
 +
 +
 +===== Visualize content of directory =====
 +
 +To visualize the content of a directory:
 +<code bash>
 +ls
 +</code>
 +
 +To visualize hidden files as well:
 +<code bash>
 +ls -a
 +</code>
 +
 +To visualize a detail list of all files (add the option ''-a'' to include also the hidden files):
 +<code bash>
 +ls -l
 +</code>
 +with the following output:
 +^ type of file ^ permissions ^ hard links ^ owner ^ group ^ size in bytes ^ date last edit ^ name ^
 +| ''d'' folder\\ ''-'' file |  owner group other|
 +
 +
 +===== Create directory =====
 +
 +To create a directory with sub-directory:
 +<code bash>
 +mkdir folder/sub-folder
 +</code>
 +
 +
 +===== Rename directory =====
 +
 +To rename a directory:
 +<code bash>
 +mv old-name/ new-name/
 +</code>
 +this will **delete** the origin folder.
 +
 +
 +===== Copy directory =====
 +
 +To copy a directory:
 +<code bash>
 +cp -r /absolute/path/from /absolute/path/to
 +</code>
 +this will **retain** the origin directory.
 +
 +
 +===== Delete empty directory =====
 +
 +To delete an empty directory:
 +<code bash>
 +rmdir folder
 +</code>
 +
 +
 +===== Delete non empty directory =====
 +
 +To delete a **non** empty directory:
 +<code bash>
 +rm -r folder
 +</code>
 +
 +
 +===== Create file =====
 +
 +To create a file:
 +<code bash>
 +touch file.txt
 +</code>
 +
 +
 +===== Rename file =====
 +
 +To rename a file:
 +<code bash>
 +mv old-file.txt new-file.txt
 +</code>
 +this will **delete** the origin file.
 +
 +
 +===== Copy file =====
 +
 +To copy a file:
 +<code bash>
 +cp /aboslute/path/from.txt /absolute/path/to.txt
 +</code>
 +this will **retain** the origin file.
 +
 +
 +
 +===== Delete file =====
 +
 +To delete a file:
 +<code bash>
 +rm file.txt
 +</code>
 +
 +
 +===== Create link =====
 +
 +To create a symbolic link (option ''-s''), which points to a file or a directory:
 +<code bash>
 +ln -s /path/origin /path/destination
 +</code>
 +
 +Note that a symbolic link is just a path so that if the file or the directory is deleted the link will still work but will point to something that doesn't exist.
 +
 +===== Change permissions =====
 +
 +The general syntax to change the permissions for a file is:
 +<code>
 +chmod <options> <people><operator><permission> <file|folder>
 +</code>
 +where:
 +^  ''<people>''  ^  ''<operator>''  ^  ''<permission>''  ^
 +| ''u'' users | ''+'' add \\ ''-'' remove \\ ''='' remove previously and set new | ''r'' read \\ ''w'' write \\ ''x'' execute |
 +| ''g'' group | ::: | ::: |
 +| ''o'' others | ::: | ::: |
 +
 +For instance:
 +<WRAP group>
 +<WRAP half column>
 +<code bash>
 +chmod ugo=rwx file.py
 +</code>
 +</WRAP>
 +
 +<WRAP half column>
 +owner, group and others: read, write and execute
 +</WRAP>
 +</WRAP>
 +
 +
 +<WRAP group>
 +<WRAP half column>
 +<code bash>
 +chmod u=rwx file.py
 +</code>
 +</WRAP>
 +
 +<WRAP half column>
 +owner: reads, writes and executes (previously permissions for group and others are removed)
 +</WRAP>
 +</WRAP>
 +
 +
 +<WRAP group>
 +<WRAP half column>
 +<code bash>
 +chmod u=xw,g=r,o=rw file.py
 +</code>
 +</WRAP>
 +
 +<WRAP half column>
 +owner: writes and reads; group: reads; others: reads and writes (for all, previously permissions are replaced with the new ones)
 +</WRAP>
 +</WRAP>
 +
 +
 +<WRAP group>
 +<WRAP half column>
 +<code bash>
 +chmod u+r,g-r,o=x file.py
 +</code>
 +</WRAP>
 +
 +<WRAP half column>
 +owner: added read permission; group: removed read permission; others: executes
 +</WRAP>
 +</WRAP>
 +
 +
 +<WRAP group>
 +<WRAP half column>
 +<code bash>
 +chmod -R o=rw folder file.py
 +</code>
 +</WRAP>
 +
 +<WRAP half column>
 +others: reads and writes (permission applied recursively at directory and file)
 +</WRAP>
 +</WRAP>
 +
 +
 +
 +===== Find string =====
 +
 +To find a string:
 +<code bash>
 +grep -ilr 'string to find' *
 +</code>
 +where:
 +^ options ^ ^
 +| ''-i'' | case insensitivity |
 +| ''-l'' | list results printing only name files |
 +| ''-r'' | recursive search  |
 +
 +<WRAP tip>
 +**Tip:** wrap the string in superscripts only if it contains blank spaces.
 +</WRAP>
 +
 +To save the output in a text file:
 +<code bash>
 +grep -ril 'string to find' * > /absolute/path/output.txt
 +</code>
 +
 +To find a string at the start of a line:
 +<code bash>
 +grep -ril '^string to find' /absolute/path
 +</code>
 +
 +To find a string at the end of a line:
 +<code bash>
 +grep -ril 'string to find$' /absolute/path
 +</code>
 +
 +
 +===== Find file =====
 +
 +To find a file in a directory hierarchy:
 +
 +<code bash>
 +find /directory file.txt
 +</code>
 +
 +<WRAP tip>
 +**Tip:** use ''sudo'' to search in directories where only the superuser has access.
 +</WRAP>
 +
 +
 +===== Processes in execution =====
 +
 +To know which processes are in execution:
 +<code bash>
 +top
 +</code>
 +
 +To stop a process:
 +<code bash>
 +kill <PID>
 +</code>
 +where ''<PID>'' is the process's ID read from the output table of the command ''top''.
 +
 +
 +===== List of logged in users =====
 +
 +To list the history of logged in users:
 +<code bash>
 +last
 +</last>
 +