1/36
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
ls command
("LS")
"List" or "Listing"
Most commands follow a simple pattern of syntax:
command [options...] [arguments...]
Arguments
An argument can be used to specify something for the command to act upon.
The " ls command " can be given the name of a directory as an argument, and it will list the contents of that directory.
aptitude command
is a package management tool available on some Linux distributions.
Options
Options can be used to alter the behavior of a command.
Listing order options
ls command prints the results in alphabetical order, so adding the -r option will print the results in reverse alphabetical order.
Multiple options can be used at once,
either given as separate options as in
-l -r
or combined like
-lr
The output of all of these examples would be the same:
Output of there are all the same: "List Reverse"
ls -l
-r ls
-rl ls
-lr
-l gives a long listing format while -r reverses the listing. The result of using both options is a long listing given in reverse order:
-v command
(verbose)
pwd command
"Locate yourself"
Printing Working Directory
-discover where you are currently located within the filesystem
Changing Directories
Directories are a type of file used to store other files-they provide a hierarchical organizational structure.
cd command
change directory
change directoryTo navigate the filesystem structure, use the cd (change directory) command to change directories.
Absolute Paths
An absolute path allows you to specify the exact location of a directory. It always starts at the root directory, therefore it always begins with the / character.
Relative Paths
A relative path gives directions to a file relative to your current location in the filesystem. Relative paths do not start with the / character, they start with the name of a directory.
<<<<< EXAMPLE: REF NOTECARD 112233 >>>>>
In the example above the cd command followed the School/Art path:
cd School/Art
<<<<< EXAMPLE: REF NOTECARD 112233 >>>>>
A path can also be broken down into multiple cd commands. The following set of commands would achieve the same results:
cd School
cd Art
Shortcuts
The . . Characters
-Regardless of which directory you are in, .. always represents one directory higher relative to the current directory, sometimes referred to as the parent directory.
The . Character
-Regardless of which directory you are in, the . character always represents your current directory. For the cd this shortcut is not very useful, but it will come in handy for commands covered in subsequent sections.
The ~ Character
-The home directory of the current user is represented by the ~ character.
>>>>>NOTECARD REF YEET-BOIS-THIS-IS-A-LIST<<<<<
The characters in question are: **(MARKED IN [BRACKETS])**
FILE TYPE
[-] rw-r--r-- 1 root root 18047 Dec 20 2017 alternatives.log
[d] rwxr-x--- 2 root adm 4096 Dec 20 2017 apache2
**The first file alternatives.log is a regular file -, while the second file apache2 is a directory d.
**The first field actually contains ten characters, where the first character indicates the type of file and the next nine specify permissions
_________________________________________________________________
Permissions
d [rwxr-xr-x] 2 root root 4096 Apr 11 2014 upstart
**Permissions indicate how certain users can access a file. Keep reading to learn more about permissions.
_________________________________________________________________
Hard Link Count
-rw-r----- [1] syslog adm 1346 Oct 2 22:17 auth.log
**This number indicates how many hard links point to this file. Hard links are beyond the scope of this module, but are covered in the NDG Linux Essentials course.
_________________________________________________________________
User Owner
-rw-r----- 1 [syslog] adm 106 Oct 2 19:57 kern.log
**User syslog owns this file. Every time a file is created, the ownership is automatically assigned to the user who created it.
_________________________________________________________________
Group Owner
-rw-rw-r-- 1 root [utmp] 292584 Oct 2 19:57 lastlog
**Indicates which group owns this file
_________________________________________________________________
File Size
-rw-r----- 1 syslog adm [19573] Oct 2 22:57 syslog
**Directories and larger files may be shown in kilobytes since displaying their size in bytes would present a very large number. Therefore, in the case of a directory, it might actually be a multiple of the block size used for the file system. Block size is the size of a series of data stored in the filesystem.
_________________________________________________________________
Timestamp
drwxr-xr-x 2 root root 4096 [Dec 7 2017] fsck
**This indicates the time that the file's contents were last modified.
Filename
-rw-r--r-- 1 root root 47816 Dec 7 20170 [bootstrap.log]
**The final field contains the name of the file or directory.
>>>>>Consider This<<<<<
In the case of symbolic links, a file that points to another file, the link name will be displayed along with an arrow and the pathname of the original file.
lrwxrwxrwx. 1 root root 22 Nov 6 2012 /etc/grub.conf
[->]../boot/grub/grub.conf
>>>>>NOTECARD REF YEET-BOIS-THIS-IS-A-LIST<<<<<
The first field actually contains ten characters, where the first character indicates the type of file and the next nine specify permissions.
The file types are:
d directory A file used to store other files.
- regular file Includes readable files, images files, binary files, and compressed files.
l symbolic link Points to another file.
s socket Allows for communication between processes.
p pipe Allows for communication between processes.
b block file Used to communicate with hardware.
c character file Used to communicate with hardware.
>> Sorting <<
By default the output of the " ls " command is sorted alphabetically by filename. It can sort by other methods as well.
Sorting List --->
Sorting List:
The -t option will sort the files by timestamp
The -S option will sort the files by file size
The -r option will reverse the order of any type of sort. Notice the difference when it is added to the previous example
Used alone the -r option with list the files in reverse alphabetical order
The su Command
su command definition: substitute user
The su command allows you to temporarily act as a different user.
It does this by creating a new shell.
The shell is simply a text input console that lets you type in commands.
By default, if a user account is not specified, the su command will open a new shell as the root user, which provides administrative privileges.
This option can be specified one of three ways:
su -
su -l
su --login
After executing the su command, a password is required
To logout and return to the sysadmin account, use the exit command.
Use the su command to switch to the root account and execute the sl command with administrative access
The sudo Command
The sudo command allows a user to execute a command as another user without creating a new shell.
*Consider This*
The sudo command can be used to switch to other user accounts as well. To specify a different user account use the -u option.
The sudo command allows a user to execute a command as another user without creating a new shell. Instead, to execute a command with administrative privileges, use it as an argument to the sudo command. Like the su command, the sudo command assumes by default the root user account should be used to execute commands.
The sudo command only provides administrative access for the execution of the specified command. This is an advantage as it reduces the risk that a user accidentally executes a command as root. The intention to execute a command is clear; the command is executed as root if prefixed with the sudo command. Otherwise, the command is executed as a regular user.
Permissions
Permissions determine the ways different users can interact with a file or directory.
_____________________________________
>>>RECALL<<<
-RW-R--R--
_____________________________________
The first - (hyphen) stands for the "File Type"
R = READ
W = WRITE
_____________________________________
RW- is the Owner's permissions
R-- is the Group's permissions
R-- is Other's permissions
When listing a file with the ls -l command, the output includes permission information.
For the example we will use a script called hello.sh located in the Documents directory:
File Type Field
[-] rw-r--r-- 1 sysadmin sysadmin 647 Dec 20 2017 hello.sh
>>The first character of this output indicates the type of a file. Recall that if the first character is a -, this is a regular file. If the character was a d, it would be a directory.
_________________________________
Permissions Field
- [rw-r--r--] 1 sysadmin sysadmin 647 Dec 20 2017 hello.sh
>>After the file type character, the permissions are displayed. The permissions are broken into three sets of three characters:
_________________________________
Owner
- [rw-] r--r-- 1 sysadmin sysadmin 647 Dec 20 2017 hello.sh
>>The first set is for the user who owns the file. If your current account is the user owner of the file, then the first set of the three permissions will apply and the other permissions have no effect.
Owner (Continued...)
>>The user who owns the file, and who these permissions apply to, can be determined by the user owner field:
-rw-r--r-- 1 [sysadmin] sysadmin 647 Dec 20 2017 hello.sh
_________________________________
Group
-rw- [r--] r-- 1 sysadmin sysadmin 647 Dec 20 2017 hello.sh
>>The second set is for the group that owns the file. If your current account is not the user owner of the file and you are a member of the group that owns the file, then the group permissions will apply and the other permissions have no effect.
Group (Continued...)
>>The group for this file can be determined by the group owner field:
-rw-r--r-- 1 sysadmin [sysadmin] 647 Dec 20 2017 hello.sh
_________________________________
Other
-rw-r-- [r--] 1 sysadmin sysadmin 647 Dec 20 2017 hello.sh
>>The last set is for everyone else, any one who that first two sets of permissions do not apply to. If you are not the user who owns the file or a member of the group that owns the file, the third set of permissions applies to you.
Permission Types
read (r)
write (w)
execute (x)
read (r) Allows for file contents to be read or copied. Without execute permission on the directory, allows for a non-detailed listing of files. With execute permission, ls -l can provide a detailed listing.
write (w) Allows for contents to be modified or overwritten. Allows for files to be added or removed from a directory.For this permission to work, the directory must also have execute permission.
execute (x) Allows for a file to be run as a process, although script files require read permission, as well. Allows a user to change to the directory if parent directories have execute permission as well.
_________________________________
Consider This
Understanding which permissions apply is an important skill set in Linux. For example, consider the following set of permissions:
-r--rw-rwx. 1 sysadmin staff 999 Apr 10 2013 /home/sysadmin/test
In this scenario, the user sysadmin ends up having less access to this file than members of the staff group or everyone else. The user sysadmin only has the permissions of r--. It doesn't matter if sysadmin is a member of the staff group; once user ownership has been established, only the user owner's permissions apply.
Changing File Permissions
chmod command
chmod really means "change the modes of access"
The chmod command is used to change the permissions of a file or directory. Only the root user or the user who owns the file is able to change the permissions of a file.
The Symbolic Method
chmod [<SET><ACTION><PERMISSIONS>]... FILE
To use the symbolic method of chmod
1.) first indicate which set of permissions is being changed:
chmod [ [[<SET>]] <ACTION><PERMISSIONS>]... FILE
2.) Next, specify an action symbol:
chmod [<SET> [[<ACTION>]] <PERMISSIONS>]... FILE
3.) After an action symbol, specify one or more permissions to be acted upon.
chmod [<SET><ACTION>< [[PERMISSIONS>]] ]... FILE
4.) Finally, a space and the pathnames for the files to assign those permissions.
chmod [<SET><ACTION><PERMISSIONS>]... [[FILE]]
1a.) SET symbols & meanings
u User: The user who owns the file.
g Group: The group who owns the file.
o Others: Anyone other than the user owner or member of the group owner.
a All: Refers to the user, group and others
________________________________
2a.) ACTION symbols & meanings
+ Add the permission, if necessary
= Specify the exact permission
- Remove the permission, if necessary
________________________________
3a.) PERMISSIONS symbols & meanings
r read
w write
x execute
Changing File Ownership
chown command
used to change the ownership of files and directories.
>>Changing the user owner requires administrative access. A regular user cannot use this command to change the user owner of a file, even to give the ownership of one of their own files to another user. However, the chown command also permits changing group ownership, which can be accomplished by either root or the owner of the file.
>>To change the user owner of a file, the following syntax can be used. The first argument, [OWNER], specifies which user is to be the new owner. The second argument, FILE, specifies which file's ownership is changing.
chown [OPTIONS] [OWNER] FILE
Viewing Files
cat command
>>which stands for "concatenate", is often used to quickly view the contents of small files.
The cat command will display the entire contents of the file, hence why it is mainly recommended for smaller files where the output is limited and does not require scrolling. To view the contents of a file using the cat command, simply type the command and use the name of the file you wish to view as the argument:
>>VIEWING A LIST<<
head [OPTIONS] [FILE]
tail [OPTIONS] [FILE]
cat alpha.txt
{Makes a list with the top half}
cat alpha.txt
(Makes a list with the bottom half}
Want a certain amount of lines? use this command:
head -n 5 alpha.txt
beep-boop-beep-beep-bop: it shows you the first five lines only
Copying Files
cp /Documents
cp [OPTIONS] SOURCE DESTINATION
cp command = copy file
* Like the " mv command ", it requires at least 2 arguments: a source & a destination. *
Example, to copy the /etc/passwd file to the current directory, use the following command:
cp /etc/passwd .
>>>LOOK!! The second argument is the " . " character. (THE SINGE PERIOD)
>>>Recall from the Changing Directories section that is a shortcut which represents the current directory.
Creating copies of files can be useful for numerous reasons:
>If a copy of a file is created before changes are made, then it is possible to revert back to the original.
>A copy of a file can be used to transfer a file to removable media devices.
>A copy of an existing document can be used as a template for a new document.
Copying Files
The dd command:
a utility for copying files or entire partitions at the bit level.
This command has several useful features, including:
It can be used to clone or delete (wipe) entire disks or partitions.
It can be used to copy raw data to removable devices, such as USB drives and CDROMs.
It can backup and restore the MBR (Master Boot Record).
It can be used to create a file of a specific size that is filled with binary zeros, which can then be used as a swap file (virtual memory).
COPYING FILES
dd command
Arguments & Descriptions
Command Syntax Example for all of the following:
dd if=/dev/zero of=/tmp/swapex bs=1M count=50
if Input File: The input file to be read from.
if=/dev/zero
The example reads from the /dev/zerofile, a special file containing an unlimited number of zeros.
of Output File: The output file to be written.
of=/tmp/swapex
bs Block Size: The block size to be used. By default, the value is considered to be in bytes.
Use the following suffixes to specify other units: K, M, G, and T for kilobytes, megabytes, gigabytes and terabytes respectively.
bs=1M
The example uses a block size of one megabyte.
count Count: The number of blocks to be read from the input file.
count=50
The example command reads 50 blocks.
Moving Files
The mv command is used to move a file from one location in the filesystem to another.
mv SOURCE DESTINATION
Example:
To move the people.csv file into the Work directory, use the filename as the source, and the directory name as the destination:
mv people.csv Work
If a file is moved from one directory to another without specifying a new name for the file, it will retain its original name. The move above can be confirmed using the ls command on the Work directory:
ls Work
Example 2:
The mv command is able to move multiple files, as long as the final argument provided to the command is the destination. For example, to move three files into the School directory:
It'll look like this:
1.) sysadmin@localhost:~/Documents$ mv numbers.txt letters.txt alpha.txt School
2.) sysadmin@localhost:~/Documents$ ls School
3.) {This list (ls) returned is the following:}
Art Engineering Math
alpha.txt letters.txt numbers.txt
Removing Files
The rm command is used to delete files and directories.
*Without any options, the rm command is typically used to remove regular files:*
It is important to keep in mind that deleted files and directories do not go into a "trash can" as with desktop-oriented operating systems. When a file is deleted with the rm command, it is almost always permanently gone.
rm [OPTIONS] FILE
The rm command will ignore directories that it's asked to remove; to delete a directory, use a recursive option, either the -r or -R options. Just be careful since these options are "recursive", this will delete all files and all subdirectories:
sysadmin@localhost:~/Documents$ rm linux.txt
sysadmin@localhost:~/Documents$ ls linux.txt
ls: cannot access linux.txt: No such file or directory
rm.linux.txt --> This command removed the file
ls linux.txt--> an attempt to view/find it in a list
answer from command prompt:
" ls: cannot access linux.txt: No such file or directory "
Filtering Input
The grep command:
A text filter that will search input and return lines which contain a match to a given pattern.
grep [OPTIONS] PATTERN [FILE]
Example:
>>Use the following command to switch to the Documents directory:
Step 1.)
sysadmin@localhost:~$ cd ~/Documents
Step 2.)
sysadmin@localhost:~/Documents$ cp /etc/passwd .
>>This file can be very large, however the " grep command " can be used filter out information about a specific user, such as the sysadmin user. >>Use sysadmin as the pattern argument and passwd as the file argument:
Step 3.)
>>>Enter the Command:
sysadmin@localhost:~/Documents$ grep sysadmin passwd
>>>Output given from command prompt:
sysadmin:x:1001:1001:System Administrator,,,,:/home/sysadmin:/bin/bash
Regular Expressions
AKA "Regex"
Regular expressions have two common forms: basic and extended.
>>Most commands that use regular expressions can interpret basic regular expressions.
>>>However, extended regular expressions are not available for all commands and a command option is typically required for them to work correctly.
The following table summarizes basic regular expression characters:
>>>>>Basic Regex Character(s) & their Meaning<<<<<
. Any one single character
[ ] Any one specified character
[^ ] Not the one specified character
* Zero or more of the previous character
^ If first character in the pattern, then pattern must be at beginning of the line to match, otherwise just a literal ^
$ If last character in the pattern, then pattern must be at the end of the line to match, otherwise just a literal $
-----------------------
The following table summarizes the extended regular expressions, which must be used with either the " egrep command " or the " -E option " with the " grep command ":
>>>>>Extended Regex Character(s) & their Meaning<<<<<
+ One or more of the previous pattern
? The preceding pattern is optional
{ } Specify minimum, maximum or exact matches of the previous pattern\
| Alternation - a logical "or"
( ) Used to create groups
Basic Patterns
>Regular expressions are patterns that only certain commands are able to interpret.
>Regular expressions can be expanded to match certain sequences of characters in text
>>The simplest of all regular expressions use only literal characters, like the example from the previous page
----------------------------
>>Example<<
Input we used:
sysadmin@localhost:~/Documents$ grep sysadmin passwd
Output given:
sysadmin:x:1001:1001:System Administrator,,,,:/home/sysadmin:/bin/bash
Anchor Characters
Anchor characters are one of the ways regular expressions can be used to narrow down search results.
Example: the pattern " root " appears many times in the " /etc/passwd " file:
We input:
sysadmin@localhost:~/Documents$ grep 'root' passwd
Computer's output:
root:x:0:0:root:/root:/bin/bash operator:x:1000:37::/root:
**To prevent the shell from misinterpreting them as special shell characters, these patterns should be protected by strong quotes, which simply means placing them between single quotes.**
Anchor Character PT 2.
The first anchor character ^ is used to ensure that a pattern appears at the beginning of the line. For example, to find all lines in /etc/passwd that start with root use the pattern ^root. Note that ^ must be the first character in the pattern to be effective.
Input:
sysadmin@localhost:~/Documents$ grep '^root' /etc/passwd
output:
root:x:0:0:root:/root:/bin/bash
Match a Single Character With " . "
Command Syntax:
sysadmin@localhost:~/Documents$ grep 'r..f' red.txt
It will match any character except for the new line character.
The pattern " r..f " would find any line that contained the letter r followed by exactly two characters
-------------------------------------------------------
Example:
>>To find all words that have at least four characters the following pattern can be used:
sysadmin@localhost:~/Documents$ grep '....' red.txt
-------------------------------------------------------
Lay man's terms: -----^
Using the " .... " pulls up 4 letter words.
-------------------------------------------------------
>>The line does not have to be an exact match, it simply must contain the pattern, as seen here when r..t is searched for in the /etc/passwd file:
Input:
sysadmin@localhost:~/Documents$ grep 'r..t' /etc/passwd
Output:
root:x:0:0:root:/root:/bin/bash operator:x:1000:37::/root:
Match a Single Character With [ ]
For example, given the " profile.txt " file:
sysadmin@localhost:~/Documents$ cat profile.txt
Hello my name is Joe. I am 37 years old. 3121991 My favorite food is avocados. I have 2 dogs. 123456789101112
To find all the lines in the profile.txt which have a number in them, use the pattern [0123456789] or [0-9]:
sysadmin@localhost:~/Documents$ grep '[0-9]' profile.txt
I am 37 years old. 3121991 I have 2 dogs. 123456789101112