Index Switzerland Zürich Italy Music BottleMD DebloatProxy Outdoor phone Outdoor baby bbs2000 Shell usage Simple Web

Basic shell and SSH usage for the impatient

This is a quick introduction on how to perform basic file system navigation and running processing jobs on Linux, also remotely via SSH. This is the tasks that would otherwise be done the Explorer (Windows), but on Linux, and remotely. It is written for Windows users. However, as a MacOSX user, you can also follow and should even quicker understand the concepts.

Content

You will learn the basics:

The basic functionality of a file system should be known: directories, working directory, shortcuts, copy/cut/paste, deletion.

For all examples, we assume you are person Alice with the user name alice

When you log in to a machine or you are already logged in and you open a shell, it will start in your home directory. With username $user, the home directory is in most cases /home/$user It corresponds to C:\Users\$user\ on Windows. Whenever you see the abbreviation ~ (a single tilde), this corresponds to your home directory and will be replaced by its path. E.g., when you type ~/Documents/, this would be replaced by /home/alice/Documents/

Note that Linux uses the slash / as a path separator, while Windows uses the backslash \

Paths

In Windows, one uses absolute path names all the time. That means, you start at the root of the file system (in Windows the drive letter) and then give the full path to where you are, e.g., C:\Users\alice\Documents\shell.docx

In Linux, there are no hard disk drives. Instead, you start at the root directory with the path / E.g., the absolute path to a document might be /home/alice/Documents/shell.docx

Absolute paths make more sense with the opposite: relative paths. Relative path names are given in relation to where you currently are in the system. E.g., after logging in and being in the home directory, you would specify Documents/shell.docx, which would expand to /home/alice/Documents/shell.docx

Absolute and relative paths are distinguished by the first letter: An absolute path always starts with a slash / , which refers to the root directory. Without a slash, the path is relative.

When in a path .. is given, this refers to the directory one level up. E.g., when you are in /home/alice/ , then .. refers to /home/ In /home/alice/Documents/ , .. points to /home/alice/

A single dot . refers to the current directory. When you are in /home/alice/Documents , then . refers just to /home/alice/Documents`

As Linux has no hard disk drives like Windows, other hard drives are mounted somewhere in the filesystem, in most cases in /mnt or in /media This means that the content of that hard drive will be in a subdirectory of these directories. E.g., one might have the C: drive of Windows mounted to /mnt/C The file from the Windows path C:\Users\alice\Documents\shell.docx would then be in /mnt/C/Users/alice/Documents/shell.docx

In all cases mentioned in this article, you can use relative as well as absolute path names without distinction!

Listing directory content (ls)

To see the content of a directory, the command ls is used (obviously it is necessary to abbreviate list...). When you type ls $path with $path being some path, it will list the content of $path E.g., ls /home/alice/Documents/ will show (among others) shell.docx The path name can also be given relatively. In /home/alice, typing ls Documents will yield the same result. With no path given, typing only ls lists the content of the current directory.

Changing directory (cd)

Using relative paths makes only sense when you know how to change the current directory. For this, the cd command is used, short for change directory. E.g., from ~, to go into the Documents/ subdirectory, you type cd Documents/ . This changes your directory from ~ to ~/Documents (equivalent to /home/alice/Documents) Using the absolute path, you would type cd ~/Documents or cd /home/alice/Documents

Copying files (cp)

The cp command will copy a file (create a copy of the original file, leaving the original untouched). The first argument is the source file, the second argument the target directory or file. When the second argument is a file, this file will be replaced with the one to be copied. E.g., cp Documents/shell.docx shell2.docx would copy the file shell.docx in the directory Documents/ to the file shell2.docx in the current directory.

When the second argument is a directory, then the source file will be copied with the same name into that directory. E.g., cp Documents/shell.docx Downloads/ will copy the file shell.docx in the directory Documents/ to a file named shell.docx in the Downloads/ directory.

Note that cp does not ask to overwrite a file! It will always overwrite!

When the source argument is a directory, you need to specify the -r flag (for recursive) to cp, i.e., the command becomes cp -r When the second argument exists and is a directory, then the source directory will be copied into that one with the same name. E.g., cp -r Documents/ Downloads/ will copy the directory Documents to Downloads/Documents/

When the second argument does not exist, but its parent directory exists, then the source directory will be renamed and copied into the parent directory. When the second argument is a file, you will get an error. E.g., to copy the Documents directory to another one called Tutorials, we type: cp -r Documents/ Tutorials/ We would then find the file ~/Tutorials/shell.docx, which would be the same as ~/Documents/shell.docx

Moving/renaming files (mv)

To move or rename a file, you use the mv command. mv works just as cp to copy files and directories, with the difference that the original file or directory is deleted in the process. mv does not need the -r flag. mv moves files and directories undistinguished.

Note that mv is also used to rename files. To rename shell.docx to shell_2.docx, one would run mv shell.docx shell_2.docx. This would leave no shell.docx, but only shell_2.docx. Of course the same works with paths deeper down, e.g., mv Documents/shell.docx shell_2.docx would remove shell.docx from the Documents directory and move it to shell_2.docx and level up.

Creating a directory (mkdir)

To create a new directory, there is one simple command: mkdir (make directory) mkdir creates a directory with the given name, so mkdir Tutorials would create the Tutorials directory in the current directory.

Delete a file or directory (rm, rmdir)

To delete a file, the command is rm (ReMove). Note that the rm command on Linux does not have a trash bin! Files deleted with rm are not recoverable!

rm deletes any files that are named after. E.g., rm shell.docx would delete the file shell.docx in the current directory. Similarly, rm Documents/shell.docx would delete the file shell.docx in the directory Documents/ below the current directory. rm /home/alice/Documents/shell.docx does the same, working with the absolute path.

You can also specify multiple files at a time, e.g., rm shell.docx shell_2.docx would delete the files shell.docx and shell_2.docx

Deleting a directory is more elaborate. When the directory is empty, you can delete it with rmdir (remove directory), e.g., rmdir Documents/ However, when the directory is not empty, an error will be printed. To delete a directory with all its content, you use the rm -r command (the -r flag stands for recursive). E.g., rm -r Documents/ will delete the directory Documents and any files that are in there and any subdirectories.

As this can easily delete files you did not explicitly specify, you have to be very careful with this command! A good approach is to always use rmdir except for when you are really sure that you want to delete a directory and all its subcontent.

SSH to work on remote computers

SSH is a protocol to connect to remote computers. Similar to Remote Deskop, just that it gives you a shell rather than a graphical desktop.

The command syntax is ssh user@host, where user is the username on the remote computer and host is the name of the remote computer. To connect as alice to the computer at 192.168.1.1, run ssh alice@192.168.1.1

This will open a shell on the remote computer. You can quickly check which computer you are on by running the hostname command, which will give you the name of the computer running the shell. By executing exit, the session will be closed and you return to the shell of your own computer.

SCP to copy data to/from reomte computers

scp copies files and directories across networked computers. It works similar to cp (see above), just that it accepts a computer address in addition to the path address. A path on a remote computer is given in the same fashion as with SSH by user@host:path So, if you want to copy the file Documents/shell.docx to 192.168.1.1 as user alice into the home directory of that user, you would run scp Documents/shell.docx alice@192.168.1.1:~ Similar to cp one needs to specify -r to copy directories. To copy the whole directory Documents, you run scp -r Documents/ julian@192.168.1.1:~

In the same way, data from remote computers can be copied to the local computer. To copy shell.docx from the remote computer to the local computer into the Downloads/ directory, you run: scp alice@192.168.1.1:~/shell.docx Downloads/ To just copy reversely Documents/ from the remote computer to the local computer in the current directory, you would run scp -r alice@192.168.1.1:Documents/ .

tmux to keep remote sessions open

When you log out of a remote computer by closing SSH or losing the connection, everything you were running is closed. As this includes long-running processing jobs, holding SSH open all the time is not really feasible.

The tool tmux prevents this (if you ever heard of screen, it works similar). tmux creates sessions that keep running when you are not connected (i.e., being attached to them).

Running tmux without any further arguments creates a new session. A bar in the lower part of the terminal describes the open windows inside a session.

By typing Ctrl+B - D (i.e., hold Ctrl at the bottom left of the keyboard and b at the same time, release, then type d), you detach from that session and you are back on the shell as before.

When reconnecting later and you want to see what happened while you were gone and continue working, you attach to that session. You do so by running tmux attach

If there is more than one session active (e.g., running tmux multiple times rather than attaching to the running session each time), you can look at the running sessions by running tmux list-sessions The first number in that list is the index. You attach to session number $N by running tmux attach -t $N

Summary

This tutorial taught you how to log in to a remote Linux system and perform basic operations to list, move and copy files.