The terminal or the command line is a powerful tool that can be used to perform a wide variety of tasks. It can be used to navigate through files and folders, run programs, and automate tasks. In robotics, the ability to use the command line effectively can come in very handy when logging into a robot remotely or debugging a robot that is not responding. Since complex robots often have full fledged operating systems running on them, the ability to work directly with the command line can be very useful.
The command line is a text interface for your computer. It's a program that takes in commands, which it passes on to the computer's operating system to run. From the command line, you can navigate through files and folders on your computer through a text-based interface and run programs without a graphical user interface (GUI).
A shell is a program whose primary purpose is to read commands and run other programs.
Some of the most popular shells are:
- Bourne Shell (Sh)
- Bourne Again Shell (Bash)
- Zsh
- Fish
Shell scripts are files that contain shell commands. They are used to automate tasks and can be run from the command line. Typically, shell scripts have the .sh extension. The first line of a shell script is called the shebang and it tells the shell which program to use to run the script. For example, the shebang #!/bin/bash tells the shell to use the Bash shell to run the script.
Example of a shell script
#!/bin/bash
echo "Hello, world!"A terminal emulator is a program that opens a window and lets you interact with the shell. There are several different terminal programs that vary across operating systems and desktop environments. Some of the most popular terminal emulators are:
- GNOME Terminal
- iTerm2
- xterm
- Konsole
ls lists the contents of a directory.
lscd changes the current working directory.
# Change to the home directory
cd
# Change to the root directory
cd /
# Change to the parent directory
cd ..
# Change to the previous directory
cd -
# Change to the directory named "foo"
cd foo
# Change to the directory named "foo" in the current directory
cd ./foo
# Change to the directory named "foo" in the parent directory
cd ../foo
# Change to the directory named "foo" in the root directory
cd /foopwd prints the current working directory.
# Print the current working directory
pwdmkdir creates a new directory.
# Create a new directory named "foo"
mkdir footouch creates a new file.
# Create a new file named "foo"
touch foocp copies a file or directory.
# Copy the file "foo" to the file "bar"
cp foo bar
# Copy the file "foo" to the directory "bar"
cp foo bar/
# Copy the directory "foo" to the directory "bar"
cp -r foo barmv moves a file or directory.
# Move the file "foo" to the file "bar"
mv foo bar
# Move the file "foo" to the directory "bar"
mv foo bar/
# Move the directory "foo" to the directory "bar"
mv foo barrm removes a file or directory.
# Remove the file "foo"
rm foo
# Remove the directory "foo"
rm -r foocat prints the contents of a file to the terminal.
# Print the contents of the file "foo"
cat fooless prints the contents of a file to the terminal one page at a time.
# Print the contents of the file "foo"
less foohead prints the first 10 lines of a file to the terminal.
# Print the first 10 lines of the file "foo"
head footail prints the last 10 lines of a file to the terminal.
# Print the last 10 lines of the file "foo"
tail foogrep searches for a pattern in a file.
# Search for the pattern "foo" in the file "bar"
grep foo barfind searches for files in a directory hierarchy.
# Search for files named "foo" in the current directory
find . -name foo
# Search for files named "foo" in the current directory and its subdirectories
find . -name foo -print
# Search for files named "foo" in the current directory and its subdirectories and delete them
find . -name foo -deletelocate searches for files in a database.
# Search for files named "foo" in the database
locate foowhich searches for a program in the directories specified by the PATH environment variable.
# Search for the program "foo" in the directories specified by the `PATH` environment variable
which fooman displays the manual page for a program.
man fooalias creates an alias for a command.
# Create an alias for the command "ls" named "l"
alias l="ls"echo prints a string to the terminal.
# Print the string "foo" to the terminal
echo fooexport sets an environment variable.
# Set the environment variable "FOO" to the value "bar"
export FOO=barsource executes a file in the current shell.
# Execute the file "foo" in the current shell
source foochmod changes the permissions of a file or directory.
# Give the user read, write and execute permissions, the group read and execute permissions and the others read and execute permissions to the file "foo"
chmod 755 foochown changes the owner and group of a file or directory.
# Change the owner of the file "foo" to the user "bar" and the group "baz"
chown bar:baz foosudo executes a command as another user, usually the superuser.
# Execute the command "foo" as the superuser
sudo foosu switches to another user.
# Switch to the user "foo"
su foopasswd changes the password of a user.
passwdexit exits the current shell.
exitclear clears the terminal screen.
clearhistory prints the command history.
historydate prints the current date and time.
dateuptime prints the system uptime.
uptimeuname prints system information.
unamedf prints disk usage information.
dfdu prints disk usage information for a file or directory.
# Print disk usage information for the file "foo"
du foo
# Print disk usage information for the directory "foo"
du foofree prints memory usage information.
freeps prints information about running processes.
pskill sends a signal to a process.
# Send the SIGTERM signal to the process with the PID 1234
kill -s SIGTERM 1234top prints information about running processes and system resource usage.
topbg resumes a suspended process in the background.
# Resume the suspended process with the PID 1234 in the background
bg 1234fg resumes a suspended process in the foreground.
# Resume the suspended process with the PID 1234 in the foreground
fg 1234jobs lists the jobs running in the background.
jobsnohup runs a command that ignores the HUP signal.
# Run the command "foo" that ignores the `HUP` signal
nohup fookillall sends a signal to all processes with a given name.
# Send the SIGTERM signal to all processes with the name "foo"
killall -s SIGTERM fooifconfig prints network interface information.
ifconfigping pings a host.
ping foo.comtraceroute traces the route taken by packets across an IP network.
traceroute foo.comwget downloads files from the internet.
# Download the file "foo" from the URL "http://example.com/foo"
wget http://example.com/foocurl transfers data from or to a server.
# Transfer data from the URL "http://example.com/foo" to the file "foo"
curl http://example.com/foo -o foossh connects to a remote server.
# Connect to the remote server "foo.com" as the user "bar"
ssh
# Connect to the remote server "foo.com" as the user "bar" and execute the command "baz"
ssh
# Connect to the remote server "foo.com" as the user "bar" and execute the command "baz" in the background
sshscp copies files between hosts on a network.
# Copy the file "foo" from the remote server "bar.com" to the current directory
scp bar.com:foo .
# Copy the file "foo" from the current directory to the remote server "bar.com"
scp foo bar.com:
# Copy the directory "foo" from the remote server "bar.com" to the current directory
scp -r bar.com:foo .
# Copy the directory "foo" from the current directory to the remote server "bar.com"
scp -r foo bar.com:rsync copies files between hosts on a network.
# Copy the file "foo" from the remote server "bar.com" to the current directory
rsync bar.com:foo .
# Copy the file "foo" from the current directory to the remote server "bar.com"
rsync foo bar.com:
# Copy the directory "foo" from the remote server "bar.com" to the current directory
rsync -r bar.com:foo .
# Copy the directory "foo" from the current directory to the remote server "bar.com"
rsync -r foo bar.com:tar creates or extracts an archive.
# Create an archive named "foo.tar" from the file "foo"
tar -cf foo.tar foo
# Extract the archive named "foo.tar" to the current directory
tar -xf foo.targzip compresses or decompresses a file.
# Compress the file "foo"
gzip foo
# Decompress the file "foo.gz"
gzip -d foo.gzgunzip decompresses a file.
# Decompress the file "foo.gz"
gunzip foo.gzzip creates or extracts a zip archive.
# Create a zip archive named "foo.zip" from the file "foo"
zip foo.zip foo
# Extract the zip archive named "foo.zip" to the current directory
unzip foo.zipunzip extracts a zip archive.
# Extract the zip archive named "foo.zip" to the current directory
unzip foo.zip