Difference between revisions of "Quickstart/Linux Command Line"
m (Pysdlb moved page Quickstart/Linux Command lLine to Quickstart/Linux Command Line without leaving a redirect: typo) |
(added in basic commands that a new user would use) |
||
Line 1: | Line 1: | ||
+ | ==The Command Line== | ||
+ | The command line in Linux is referred to as a shell. The shell is a program that allows the user to interact with Linux at the command line. In true Linux style, there are a few different ones to choose from. However, the one used predominantly is BASH. The name BASH is an acronym for “Bourne Again SHell”, a reference to BASH is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne. | ||
+ | Below is an example of Viper's command-line interface using the terminal program [http://mobaxterm.mobatek.net/ MobaXterm]. | ||
+ | |||
+ | [[File:commandline.jpg]] | ||
+ | |||
+ | ==Directories== | ||
+ | As with other operating systems such as Microsoft Windows the filesystem is based around files and directories. Linux is no exception to this and uses a number of commands for the user to navigate around its own filesystem. | ||
+ | |||
+ | ===pwd=== | ||
+ | On the command line pwd (or print working directory) basically displays the current directory you are in. This would appear as: | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ pwd | ||
+ | /home/username | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ===cd=== | ||
+ | On the command line cd (or change directory) changes your current directory to the one specified: | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ cd /var | ||
+ | username@viper:~$ pwd | ||
+ | /var | ||
+ | username@viper:~$ cd /home/user | ||
+ | username@viper:~$ pwd | ||
+ | /home/username | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | There is also a shortcut back to your home directory by typing the character ~ (tilda) which has the same effect as typing (for example) /home/user. | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ cd ~ | ||
+ | username@viper:~$ pwd | ||
+ | /home/username | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | To go to the directory above (or parent directory), we use the characters .. | ||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ pwd | ||
+ | /home/username | ||
+ | |||
+ | username@viper:~$ cd .. | ||
+ | username@viper:~$ pwd | ||
+ | /home | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ===ls=== | ||
+ | This command lists the contents of a directory: | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ ls | ||
+ | myfile1.txt myfile2.txt mydirectory1 | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ls also has several options: | ||
+ | *'''ls -l''' shows a long listing with more information | ||
+ | *'''ls -a''' shows all files including those that are hidden | ||
+ | *'''ls -la''' shows a combination of the options above | ||
+ | *'''ls *(file type)''' shows all files with that file type (eg: ls *py will show all python files) | ||
+ | |||
+ | |||
+ | ===mkdir=== | ||
+ | This commands makes a directory from the specified directory: | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ mkdir mydirectory1 | ||
+ | username@viper:~$ cd mydirectory1 | ||
+ | username@viper:~$ pwd | ||
+ | /home/user/mydirectory1 | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | === rmdir === | ||
+ | |||
+ | This command removes the specified directory, note the directory must be empty and must not be the directory you are currently in: | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ rmdir mydirectory1 | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ==Files== | ||
+ | Files on Linux are case sensitive. This means that FILE1 is different from file1, and /etc/hosts are different from /etc/Hosts (the latter one does not exist on a typical Linux computer). | ||
+ | |||
+ | Everything on Linux is a file. A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1), and any process are all represented somewhere in the file system as a file. | ||
+ | |||
+ | ===touch=== | ||
+ | This creates an empty file, which can be useful for various uses. | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ touch newfile.c | ||
+ | username@viper:~$ ls | ||
+ | newfile.c | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ===cp=== | ||
+ | Copy files or directories from a source to a destination: | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ cp parallel.c mybackup.c (copies parallel.c to mybackup.c) | ||
+ | username@viper:~$ cp parallel.c mydirectory1 (copies parallel.c to mydirectory1) | ||
+ | username@viper:~$ cp *c backupdirectory/ (copies all *.c files to backupdirectory) | ||
+ | username@viper:~$ cp –r mydirectory1 mydirectory2 (copies one directory to another, note the option –r for recursive copying) | ||
+ | |||
+ | </pre> | ||
+ | ===mv=== | ||
+ | Move files from a source to a destination. A versatile command that can rename a file too: | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ mv file1.c testfile.c (rename file1.c to testfile.c) | ||
+ | username@viper:~$ mv directory1 directory2 (rename directory) | ||
+ | username@viper:~$ mv file1.c /home/user/myrepo (mv file1.c to /home/user/myrepo/file1.c) | ||
+ | |||
+ | </pre> | ||
+ | ===cat=== | ||
+ | The '''cat''' command (short for concatenate) one of the most universal tools, yet all it does is copy standard input to standard output. | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ cat /etc/resolv.conf | ||
+ | |||
+ | domain example.com | ||
+ | search example.com | ||
+ | nameserver 192.168.1.42 | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ===rm=== | ||
+ | Remove a file, as always be very careful with this command and without a backup, this file will be lost forever. | ||
+ | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | |||
+ | username@viper:~$ ls | ||
+ | newfile.c backup.c | ||
+ | username@viper:~$ rm newfile.c | ||
+ | username@viper:~$ ls | ||
+ | backup.c | ||
+ | |||
+ | You can use rm -r directory_name to remove directories that are not empty. | ||
+ | </pre> |
Revision as of 11:45, 8 November 2022
Contents
The Command Line
The command line in Linux is referred to as a shell. The shell is a program that allows the user to interact with Linux at the command line. In true Linux style, there are a few different ones to choose from. However, the one used predominantly is BASH. The name BASH is an acronym for “Bourne Again SHell”, a reference to BASH is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.
Below is an example of Viper's command-line interface using the terminal program MobaXterm.
Directories
As with other operating systems such as Microsoft Windows the filesystem is based around files and directories. Linux is no exception to this and uses a number of commands for the user to navigate around its own filesystem.
pwd
On the command line pwd (or print working directory) basically displays the current directory you are in. This would appear as:
username@viper:~$ pwd /home/username
cd
On the command line cd (or change directory) changes your current directory to the one specified:
username@viper:~$ cd /var username@viper:~$ pwd /var username@viper:~$ cd /home/user username@viper:~$ pwd /home/username
There is also a shortcut back to your home directory by typing the character ~ (tilda) which has the same effect as typing (for example) /home/user.
username@viper:~$ cd ~ username@viper:~$ pwd /home/username
To go to the directory above (or parent directory), we use the characters ..
username@viper:~$ pwd /home/username username@viper:~$ cd .. username@viper:~$ pwd /home
ls
This command lists the contents of a directory:
username@viper:~$ ls myfile1.txt myfile2.txt mydirectory1
ls also has several options:
- ls -l shows a long listing with more information
- ls -a shows all files including those that are hidden
- ls -la shows a combination of the options above
- ls *(file type) shows all files with that file type (eg: ls *py will show all python files)
mkdir
This commands makes a directory from the specified directory:
username@viper:~$ mkdir mydirectory1 username@viper:~$ cd mydirectory1 username@viper:~$ pwd /home/user/mydirectory1
rmdir
This command removes the specified directory, note the directory must be empty and must not be the directory you are currently in:
username@viper:~$ rmdir mydirectory1
Files
Files on Linux are case sensitive. This means that FILE1 is different from file1, and /etc/hosts are different from /etc/Hosts (the latter one does not exist on a typical Linux computer).
Everything on Linux is a file. A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1), and any process are all represented somewhere in the file system as a file.
touch
This creates an empty file, which can be useful for various uses.
username@viper:~$ touch newfile.c username@viper:~$ ls newfile.c
cp
Copy files or directories from a source to a destination:
username@viper:~$ cp parallel.c mybackup.c (copies parallel.c to mybackup.c) username@viper:~$ cp parallel.c mydirectory1 (copies parallel.c to mydirectory1) username@viper:~$ cp *c backupdirectory/ (copies all *.c files to backupdirectory) username@viper:~$ cp –r mydirectory1 mydirectory2 (copies one directory to another, note the option –r for recursive copying)
mv
Move files from a source to a destination. A versatile command that can rename a file too:
username@viper:~$ mv file1.c testfile.c (rename file1.c to testfile.c) username@viper:~$ mv directory1 directory2 (rename directory) username@viper:~$ mv file1.c /home/user/myrepo (mv file1.c to /home/user/myrepo/file1.c)
cat
The cat command (short for concatenate) one of the most universal tools, yet all it does is copy standard input to standard output.
username@viper:~$ cat /etc/resolv.conf domain example.com search example.com nameserver 192.168.1.42
rm
Remove a file, as always be very careful with this command and without a backup, this file will be lost forever.
username@viper:~$ ls newfile.c backup.c username@viper:~$ rm newfile.c username@viper:~$ ls backup.c You can use rm -r directory_name to remove directories that are not empty.