[GUIDE] Understanding Linux / Terminal Command Essentials
#1
[Image: Jm9Fe.png]


Some Things You Should Know About The Linux CLI.

Everything On Linux Is a File.

Everything on Linux is a file, from your normal text documents, mp3's, documents, 
pictures, videos, to things like directories, disk drives, processes, and even 
network connections! Linux just views everything as a stream of data, 
bits and bytes, 0's and 1's, the programs running on Linux, such as a text edito

or a media player are what know how to handle the files that Linux views as just files.

Linux File Names.

You are allowed to use up to 255 characters plus a three letter file extension for a 
file name on Linux, which means you can be as descriptive as possible, but it is 
good to keep the file name under 80 characters because the average terminal is 
80 characters wide.
All file names in Linux are case-sensitive, meaning ilovelinux.txt, ILoveLinux.txt, 
and iLOvElInuX.txt are all completely different to linux, you could have all three of
those names as different files in the same directory. If you are using the terminal 
to specify a file you need to be sure that all the lower-case and upper-case letters 
are the same. Commands are all case-sensitive as well, the command rm won't
work if you type Rm or rM or RM, it will only work as rm.
Some special characters in Linux shouldn't be used when naming files, avoid using all
of these characters:

Code:


Code:
\ / - [ ] * ? ' "

and spaces just because they all cause problems when trying to access them via 
the terminal, if you do use them however you can use quotation marks while specifying 
the file via terminal eg: rm "doilovelinux*?.txt". Letters, underscores as long 
as it's directly in between two letters, and numbers are always safe for file names.

Wildcards in Linux.

There are 3 different wildcards in linux, ? * and [ ] and I will go ove

what they do in a minute. Lets say you have 12 pictures and 1 text file in one directory 
with the following names:
linux1.jpg
linux2.jpg
linux3.jpg
linux4.jpg
linux5.jpg
linux6.jpg
linux7.jpg
linux8.jpg
linux9.jpg
linux10.jpg
linux11.jpg
linux12.jpg
linux1.txt

and you want to delete them using the command rm
Wildcards allow you to specify more than one file at a time by matching characters.
The * wildcard will match any character 0 or more times, here are some examples:

Code:


Code:
rm linux1*.jpg will delete linux1-12.jpg.
rm linux*.jpg will delete linux1-12.jpg but leave the .txt alone.
rm *txt will delete linux.txt but none of the .jpg.
rm linux* will delete all linux1-12.jpg and linux1.txt.
rm * will delete all files in the directory.

The ? wildcard will match a single character, here are some examples:
rm linux1?.jpg will delete linux10-12.jpg
rm linux?.jpg will delete linux1-9.jpg
rm linux?.* will delete linux1-9.jpg and linux1.txt

Lastly, the [ ] wildcard will match a set of single characters or a range of characters 
separated by a hyphen. Here are a few examples:

Code:


Code:
rm linux1[12].jpg will delete linux11.jpg and linux12.jpg but not linux10.jpg.
rm linux1[0-2].jpg will delete linux10.jpg, linux11.jpg, and linux12.jpg but not linux1.jpg.
rm linux[4-8].jpg will delete linux4.jpg through linux8.jpg.


The ls command

"ls" is one of the most commonly used commands. ls by itself just lists the contents of 
the directory you are currently in. Here are some examples:

Code:

Code:
ls /home/downloads
lists contents of /home/downloads

~ is equal to /home/USER
~downloads/ equals to /home/downloads
~/downloads equals to /home/USER/downloads



Code:

Code:
ls ~/downloads
lists contents of /home/downloads


Code:

Code:
ls ~/downloads/*.exe
lists all .exe file types in the downloads folder

Code:
apt-get
Gets a file(s) from a source or website.

Code:
apt-get install
Installs files from a source or website.

Code:
locate
Locates a file on your system.

Code:
wget
Downloads a web page. Usage: wget [site.com]

Code:

Code:
ls -R ~/downloads
lists all files in downloads and all of the files in its subfolders.


Code:

Code:
ls -1 ~/downloads
(1=one, not l)
Organises the output in one column.


Code:

Code:
ls -m ~/downloads
Separates output with commas.


Code:

Code:
ls -a ~/downloads
View all of the hidden files and folders.


Code:

Code:
ls -F ~/downloads
View all of the contents file types.
* = executable
/ = directory
@ = symbolic link
| = FIFO
= = socket


Code:

Code:
ls --color
Displays a different color for each file type in the output.
Default shell color = regular file
Green = Executable
Blue = Directory
Magenta = Symbolic link or socket or image files/ audio files
Yellow = FIFO
Red = Archive


Code:

Code:
ls -l
The lowercase L is short for long, and it will give you a lot of info about who owns the file, 
permissions set, size etc.


Code:

Code:
ls -X
Sorts the output by it's file extension type, eg. exe, jpg, mp3 etc.


Code:

Code:
ls -t
Sorts the output by the date/time of the last modification.


Code:

Code:
ls -S
Sorts the output by it's file size.


Code:

Code:
ls -h
Gives your output K, M, and G sizes, so it's easier to read file sizes.

Now that's all for ls commands, try mixing them up a bit, how can you mix them up? 
Would you have to do:

Code:

Code:
ls -l -a -S -h
o

ls -laSh
Try playing with it a little bit and see what you can do and what you like best.

Now lets get into moving around in the directories and changing them!


First lets start with the pwd command, you may think it means password but it means 
"Print Working Directory" Go to your terminal and type it in and see what happens!

Next is cd, which stands for change directory.

Start out by typing:

Code:

Code:
cd /Desktop
and it should take you to your desktop directory, you can do this in any directory.


Code:

Code:
cd ~
Will take you to your home directory.


Code:

Code:
cd -
Will take you to the directory you were previously at.


Changing, making, and deleting files and directories (WHICH ARE ALSO FILES DON'T FORGET!)


The touch command.
go to your terminal and try out the touch command using this syntax:

Code:

Code:
touch filename.ext
and it will change it's modification date to the current date/time.

But what if you specify a non-existant file? Try using:

Code:

Code:
touch test.txt
it will create an empty text file called test.

To make a new directory you would use the mkdir command, which is short for make a directory.

Lets make a test directory, and call it test, you would type in:


Code:

Code:
mkdir test
This would make a directory called "test"
what if you wanted to make a directory with sub-directories? 
you would use:


Code:

Code:
mkdir -p this/is/a/test
and it would make 4 directories in that order one after the other inside of the previous.

try doing that last command but like this instead:

Code:

Code:
mkdir -pv this/is/a/test
the v perimeter shows you what mkdir is doing while it does it. 

Copying files via the cp command.
Lets start out with the basic syntax:

Code:

Code:
cp test.txt test_bak.txt
It will copy the file test.txt to the same directory but named test_bak.txt.
So you just made a backup of test.txt!


Code:

Code:
cp -v
Copies files verbosely.


Code:

Code:
cp -i
Stops you from copying over important files.


Code:

Code:
cp -R directory_name new_dir_name
Using that syntax allows you to copy an entire directory.


Code:

Code:
cp -A directory_name new_backup_dir_name
Copies the content of one directory to a perfect backup copy directory.

Moving files around.

To move files you would use the mv command, short for move.

mv has mainly only one syntax we will go over for now:


Code:

Code:
mv file_to_move place/to/move/to
That would move a file to the place you specified it to go!

Now lets delete some files using the rm command, which is short for remove.

So first make an empty directory named test, and an empty file named test.txt 
with the commands we went over earlier. Now lets delete them using the rm command!
first lets delete the .txt file, we would use:

Code:

Code:
rm test.txt
simple right? Now TRY and delete the test directory with the rm command!

There are a lot of different things you can do with rm and I'll go over them briefly:


Code:

Code:
rm -v
Removes files verbosely.


Code:

Code:
rm -i
Stops you from deleting important files.


Code:

Code:
rmdir
Lets you delete empty directories.(go back and delete the test directory if you want now.


Code:

Code:
rm -DemonForums
Will delete anything you specify! Folders with files in them, system files etc.


Last but not least!!!


Lets learn about the su command. Short for super user! Just kidding it stands for switch 
user. But it can make you a super user! Lets start by switching to another user:


Code:

Code:
su username
Would switch you to that account assuming you had the proper credentials.

Now try the previous command with -l


Code:

Code:
su -l <username>
It makes the system think you are "username" when you are still the user you logged in as.

Want to look like root? (root is the administrator of a *nix box)
type:

Code:

Code:
su
Is basically the same as typing

Code:

Code:
su -l root

Want to actually become root and not just steal his style?
First type:

Code:

Code:
dir
to list the directories you're in now(not required)
then:

Code:

Code:
su -
and run the dir command again and see what changed? You're now the root of your box.

Want to learn more about these commands? Try using these simple terminal tools.

Code:

Code:
COMMAND --help
man COMMAND
both give you more specific details about "command"

[-] The following 1 user Likes AFG's post:
  • DStar
Reply
#2
Oh wow this was very very good and understandable, sir Big Grin

I'd love to see more about Linux and possibly Python!
Thanks OP Heart
Reply
#3
(10-09-2016, 02:17 AM)AFG Wrote:
[Image: Jm9Fe.png]


Some Things You Should Know About The Linux CLI.

Everything On Linux Is a File.

Everything on Linux is a file, from your normal text documents, mp3's, documents, 
pictures, videos, to things like directories, disk drives, processes, and even 
network connections! Linux just views everything as a stream of data, 
bits and bytes, 0's and 1's, the programs running on Linux, such as a text edito

or a media player are what know how to handle the files that Linux views as just files.

Linux File Names.

You are allowed to use up to 255 characters plus a three letter file extension for a 
file name on Linux, which means you can be as descriptive as possible, but it is 
good to keep the file name under 80 characters because the average terminal is 
80 characters wide.
All file names in Linux are case-sensitive, meaning ilovelinux.txt, ILoveLinux.txt, 
and iLOvElInuX.txt are all completely different to linux, you could have all three of
those names as different files in the same directory. If you are using the terminal 
to specify a file you need to be sure that all the lower-case and upper-case letters 
are the same. Commands are all case-sensitive as well, the command rm won't
work if you type Rm or rM or RM, it will only work as rm.
Some special characters in Linux shouldn't be used when naming files, avoid using all
of these characters:

Code:


Code:
\ / - [ ] * ? ' "

and spaces just because they all cause problems when trying to access them via 
the terminal, if you do use them however you can use quotation marks while specifying 
the file via terminal eg: rm "doilovelinux*?.txt". Letters, underscores as long 
as it's directly in between two letters, and numbers are always safe for file names.

Wildcards in Linux.

There are 3 different wildcards in linux, ? * and [ ] and I will go ove

what they do in a minute. Lets say you have 12 pictures and 1 text file in one directory 
with the following names:
linux1.jpg
linux2.jpg
linux3.jpg
linux4.jpg
linux5.jpg
linux6.jpg
linux7.jpg
linux8.jpg
linux9.jpg
linux10.jpg
linux11.jpg
linux12.jpg
linux1.txt

and you want to delete them using the command rm
Wildcards allow you to specify more than one file at a time by matching characters.
The * wildcard will match any character 0 or more times, here are some examples:

Code:


Code:
rm linux1*.jpg will delete linux1-12.jpg.
rm linux*.jpg will delete linux1-12.jpg but leave the .txt alone.
rm *txt will delete linux.txt but none of the .jpg.
rm linux* will delete all linux1-12.jpg and linux1.txt.
rm * will delete all files in the directory.

The ? wildcard will match a single character, here are some examples:
rm linux1?.jpg will delete linux10-12.jpg
rm linux?.jpg will delete linux1-9.jpg
rm linux?.* will delete linux1-9.jpg and linux1.txt

Lastly, the [ ] wildcard will match a set of single characters or a range of characters 
separated by a hyphen. Here are a few examples:

Code:


Code:
rm linux1[12].jpg will delete linux11.jpg and linux12.jpg but not linux10.jpg.
rm linux1[0-2].jpg will delete linux10.jpg, linux11.jpg, and linux12.jpg but not linux1.jpg.
rm linux[4-8].jpg will delete linux4.jpg through linux8.jpg.


The ls command

"ls" is one of the most commonly used commands. ls by itself just lists the contents of 
the directory you are currently in. Here are some examples:

Code:

Code:
ls /home/downloads
lists contents of /home/downloads

~ is equal to /home/USER
~downloads/ equals to /home/downloads
~/downloads equals to /home/USER/downloads



Code:

Code:
ls ~/downloads
lists contents of /home/downloads


Code:

Code:
ls ~/downloads/*.exe
lists all .exe file types in the downloads folder


Code:

Code:
ls -R ~/downloads
lists all files in downloads and all of the files in its subfolders.


Code:

Code:
ls -1 ~/downloads
(1=one, not l)
Organises the output in one column.


Code:

Code:
ls -m ~/downloads
Separates output with commas.


Code:

Code:
ls -a ~/downloads
View all of the hidden files and folders.


Code:

Code:
ls -F ~/downloads
View all of the contents file types.
* = executable
/ = directory
@ = symbolic link
| = FIFO
= = socket


Code:

Code:
ls --color
Displays a different color for each file type in the output.
Default shell color = regular file
Green = Executable
Blue = Directory
Magenta = Symbolic link or socket or image files/ audio files
Yellow = FIFO
Red = Archive


Code:

Code:
ls -l
The lowercase L is short for long, and it will give you a lot of info about who owns the file, 
permissions set, size etc.


Code:

Code:
ls -X
Sorts the output by it's file extension type, eg. exe, jpg, mp3 etc.


Code:

Code:
ls -t
Sorts the output by the date/time of the last modification.


Code:

Code:
ls -S
Sorts the output by it's file size.


Code:

Code:
ls -h
Gives your output K, M, and G sizes, so it's easier to read file sizes.

Now that's all for ls commands, try mixing them up a bit, how can you mix them up? 
Would you have to do:

Code:

Code:
ls -l -a -S -h
o

ls -laSh
Try playing with it a little bit and see what you can do and what you like best.

Now lets get into moving around in the directories and changing them!


First lets start with the pwd command, you may think it means password but it means 
"Print Working Directory" Go to your terminal and type it in and see what happens!

Next is cd, which stands for change directory.

Start out by typing:

Code:

Code:
cd /Desktop
and it should take you to your desktop directory, you can do this in any directory.


Code:

Code:
cd ~
Will take you to your home directory.


Code:

Code:
cd -
Will take you to the directory you were previously at.


Changing, making, and deleting files and directories (WHICH ARE ALSO FILES DON'T FORGET!)


The touch command.
go to your terminal and try out the touch command using this syntax:

Code:

Code:
touch filename.ext
and it will change it's modification date to the current date/time.

But what if you specify a non-existant file? Try using:

Code:

Code:
touch test.txt
it will create an empty text file called test.

To make a new directory you would use the mkdir command, which is short for make a directory.

Lets make a test directory, and call it test, you would type in:


Code:

Code:
mkdir test
This would make a directory called "test"
what if you wanted to make a directory with sub-directories? 
you would use:


Code:

Code:
mkdir -p this/is/a/test
and it would make 4 directories in that order one after the other inside of the previous.

try doing that last command but like this instead:

Code:

Code:
mkdir -pv this/is/a/test
the v perimeter shows you what mkdir is doing while it does it. 

Copying files via the cp command.
Lets start out with the basic syntax:

Code:

Code:
cp test.txt test_bak.txt
It will copy the file test.txt to the same directory but named test_bak.txt.
So you just made a backup of test.txt!


Code:

Code:
cp -v
Copies files verbosely.


Code:

Code:
cp -i
Stops you from copying over important files.


Code:

Code:
cp -R directory_name new_dir_name
Using that syntax allows you to copy an entire directory.


Code:

Code:
cp -A directory_name new_backup_dir_name
Copies the content of one directory to a perfect backup copy directory.

Moving files around.

To move files you would use the mv command, short for move.

mv has mainly only one syntax we will go over for now:


Code:

Code:
mv file_to_move place/to/move/to
That would move a file to the place you specified it to go!

Now lets delete some files using the rm command, which is short for remove.

So first make an empty directory named test, and an empty file named test.txt 
with the commands we went over earlier. Now lets delete them using the rm command!
first lets delete the .txt file, we would use:

Code:

Code:
rm test.txt
simple right? Now TRY and delete the test directory with the rm command!

There are a lot of different things you can do with rm and I'll go over them briefly:


Code:

Code:
rm -v
Removes files verbosely.


Code:

Code:
rm -i
Stops you from deleting important files.


Code:

Code:
rmdir
Lets you delete empty directories.(go back and delete the test directory if you want now.


Code:

Code:
rm -DemonForums
Will delete anything you specify! Folders with files in them, system files etc.


Last but not least!!!


Lets learn about the su command. Short for super user! Just kidding it stands for switch 
user. But it can make you a super user! Lets start by switching to another user:


Code:

Code:
su username
Would switch you to that account assuming you had the proper credentials.

Now try the previous command with -l


Code:

Code:
su -l <username>
It makes the system think you are "username" when you are still the user you logged in as.

Want to look like root? (root is the administrator of a *nix box)
type:

Code:

Code:
su
Is basically the same as typing

Code:

Code:
su -l root

Want to actually become root and not just steal his style?
First type:

Code:

Code:
dir
to list the directories you're in now(not required)
then:

Code:

Code:
su -
and run the dir command again and see what changed? You're now the root of your box.

Want to learn more about these commands? Try using these simple terminal tools.

Code:

Code:
COMMAND --help
man COMMAND
both give you more specific details about "command"

Nice, would it also be good if you added things like:

apt-get
install
chmod
[-] The following 1 user Likes DStar's post:
  • AFG
Reply
#4
Very good high quality tutorial, but people need to know about wget! YusHeart

[Image: H4JQn0Z.gif]
Reply
#5
How could I have missed this?! Great stuff as always, @AFG thanks for sharing this one. Heart

Are you a guest to this site? Click the image below and sign up today!
[Image: FTYbRmR.gif]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)