Linux
Linux commands
By Juan Felipe Martínez Bedoya
These are the main commands for using the Linux terminal.
Basic commands
Moving between folders: cd
cd folderName # enter a folder
cd .. # go up to the parent folder
cd ../folder/subfolder # go to a specific path
Listing files: ls
ls # files and folders in the current path
ls -la # includes hidden files (those starting with a dot)
Showing a file’s contents: cat
cat exampleFile.txt
Finding out where you are: pwd
pwd
Creating an empty folder: mkdir
mkdir folderName
Creating and editing files: touch and vi
touch creates an empty file of any type: plain text (.txt) or programs (.py, .ino…). To edit it from the terminal, use the vi editor, which has its own tutorial.
touch exampleFile.txt # create the empty file
vi exampleFile.txt # open it for editing
Moving or renaming: mv
The first parameter is what you move and the second is the destination. It is also used to rename.
mv exampleFile.txt destination/
mv exampleFolder/ destination/
mv old.txt new.txt # rename
Copying: cp
cp exampleFile.txt copy.txt
cp -r exampleFolder/ folderCopy/ # -r to copy folders
Deleting: rm
rm exampleFile.txt
rm -r exampleFolder/ # -r to delete folders
Be careful: rm does not send files to the trash, it deletes them permanently.
mv, cp and rm all accept paths such as parent/child/file.txt.
More advanced commands
Permissions: chmod
Permissions say who can read (r), write (w) or execute (x) a file. There are three kinds of user: the owner (u), the group (g) and others (o). The general form is:
chmod arguments exampleFile.txt
Symbolic method. State who (u, g, o, or a for all), whether to add (+) or remove (-), and which permission:
chmod o-x example.py # remove execute permission for others
chmod a+rwx example.py # give every permission to everyone
Numeric method. Write three digits: owner, group and others. Each digit is the sum of read (4), write (2) and execute (1). For example, 5 = read and execute, and 7 = all permissions.
chmod 754 example.py # owner: all; group: read and execute; others: read only
Bash files
Bash files contain commands you would normally type in the terminal. They are the equivalent of Windows .bat files. They have their own tutorial.
Filtering output: grep
The output of any command can be filtered with grep through a pipe (|). For example, to list only the files ending in .py:
ls | grep '\.py$'
The pipe | sends the output of the first command as input to the second.
Environment variables
Environment variables are values that affect how programs run, for example the paths where the system looks for executables. To see them all, use export (or env), and filter them with grep:
export | grep ROS
To define a variable (no spaces around the =):
export EXAMPLE="Hello"
System processes
Linux has foreground processes, which take over a terminal and wait for user input, and background processes, which are not attached to a terminal. To see active processes use htop (more detailed) or ps faux.
- Ctrl+C ends the foreground process.
- Ctrl+Z pauses the foreground process. Then
bgresumes it in the background andfgbrings it back to the foreground. killends a process by its identifier (PID), which you can see withhtoporps.pkilldoes it by name.
kill 1234 # by PID
pkill processName # by nameTranslated from Spanish.