Sunday, 29 July 2018

Copy (cp) File And Directory Examples

LPI Certification, LPI Guides, LPI Tutorial and Material, LPI Learning

Copy (cp) is the frequently used command in Unix (or Linux). The cp Command is used to copy the files from one directory to another directory. The cp command can also be used to copy the directories also. The syntax of cp command is

cp [options] source destination

Examples of cp Command


1. Write a unix/linux cp command to copy file in to a directory?


The basic usage of cp command is to copy a file from the current directory to another directory.

cp sum.pl tmp/

The cp command copies the file sum.pl into the tmp directory. The cp command does not remove the source file. It just copies the file into a new location. If a file with the same name as the source exists in the destination location, then by default the cp command overwrites that new file

2. Write a unix/linux cp to prompt for user before overwriting a file ( Interactive cp command)?


The -i option to the cp command provides the ability to prompt for a user input whether to overwrite the destination file or not.

> cp sum.pl tmp/
cp: overwrite `tmp/sum.pl'?

If you enter y, then the cp command overwrites the destination file, otherwise the cp command does not copy the file.

3. Write a unix/linux cp command to copy multiple files in to a new directory?


You can specify multiple files as the source and can copy to the new location.

cp log.dat bad.dat tmp/

The cp command copies the log.dat, bad.dat files in the current directory to the tmp directory.

4. Write a unix/linux cp command to do a Regular expression copy?


You can copy a set of files by specifying a regular expression pattern.

cp *.dat tmp/

Here the cp command copies all the files which has "dat" as suffix to the destination directory.

5. Write a unix/linux cp command to copy a file in to the current directory?


You can copy a file from a different directory to the current directory.

cp /usr/local/bin/multiply.sh.

Here the cp command copies the multiply.sh file in the /usr/local/bin directory the current directory. The dot (.) indicates the current directory.

6. Write a unix/linux cp command to copy all the files in a directory?


The cp command can be used to copy all the files in directory to another directory.

cp docs/* tmp/

This command copies all the files in the docs directory to the tmp directory.

7. Write a unix/linux cp command to copy files from multiple directories?


You can copy the files from different directories into a new location.

cp docs/* scripts/* tmp/

The command copies the files from docs and script directories to the destination directory tmp.

8. Write a unix/linux cp command to Copy a directory.


You can recursively copy a complete directory and its sub directory to another location using the cp command

cp -r docs tmp/

This copies the complete directory docs into the new directory tmp

9. Write a unix/linux cp command to Forcibly copy a file with -f option?


You can force the cp command to copy an existing destination file even it cannot be opened.

cp -f force_file.txt /var/tmp/

Friday, 27 July 2018

ls Command in Unix and Linux Examples

ls Command, Linux Command, Unix Command, LPI Study Materials

ls is the most widely used command in unix or linux. ls command is used to list the contents of a directory. Learn the power of ls command to make your life easy. The syntax of ls command is

ls [options] [pathnames]


1. Write a unix/linux ls command to display the hidden files and directories?


To display the hidden files and directories in the current directory use the -a option of the ls command.

> ls -a
.  ..  documents  .hidden_file  sum.pl

Hidden files are the one whose name starts with dot (.). The las -a displays the current directory (.) and parent directory (..) also. If you want to exclude the current directory, parent directory, then use -A option.

> ls -A
documents  .hidden_file  sum.pl

2. Write a unix/linux ls command to classify the files with special characters


The -F option to ls command classifies the files. It marks the

◈ Directories with trailing slash (/)
◈ Executable files with trailing asterisk (*)
◈ FIFOs with trailing vertical bar (|)
◈ Symbolic links with trailing at the rate sign (@)
◈ Regular files with nothing

> ls -F
documents/  sum.pl link@

3. Write a unix/linux ls command to print each file in a separate line?


The -1 option to the ls command specifies that each file should be displayed on a separate line

> ls -1
documents
sum.pl

4. Write a unix/linux ls command to display the inode number of file?


In some cases, you want to know the inode number of a file. Use -i option to the ls command to print the inode number of a file.

> ls -i1
10584066 documents
3482450 sum.pl

5. Write a unix/linux ls command to display complete information about the files?


The -l option provides lots of information about the file type, owner, group, permissions, file size, last modification date.

> ls -l
total 16
drwxr-xr-x 2 matt db 4096 Jan 30 23:08 documents
-rw-r--r-- 1 matt db   49 Jan 31 01:17 sum.pl

◈ The first character indicates the type of the file. - for normal file, d for directory, l for link file and s for socket file
◈ The next 9 characters in the first field represent the permissions. Each 3 characters refers the read (r), write (w), execute (x) permissions on owner, group and others. - means no permission.
◈ The second field indicates the number of links to that file.
◈ The third field indicates the owner name.
◈ The fourth field indicates the group name.
◈ The fifth field represents the file size in bytes.
◈ The sixth field represents the last modification date and time of the file.
◈ And finally the seventh field is the name of the file.

6. Write a unix/linux ls command to sort the files by their modification time?


The -t option allows the ls command to sort the files in descending order based on the modification time.

> ls -t1
sum.pl
documents

7. Write a unix/linux ls command to sort the files in ascending order of modification time?


The -r option reverses the order of the files displayed. Combine the -t and -r options to sort the files in ascending order.

> ls -rt1
documents
sum.pl

8. Write a unix/linux ls command to print the files recursively?


So far the ls command prints the files in the current directory. Use the -R option to recursively print the files in the sub-directories also.

> ls -R
.:
documents  sum.pl

./documents:
file.txt

9. Write a unix/linux ls command to print the files in a specific directory?


You can pass a directory to the ls command as an argument to print for the files in it.

> ls /usr/local/bin

10. Write a unix/linux ls command to display files in columns?


The -x option specifies the ls command to display the files in columns.

> ls -x

Tuesday, 24 July 2018

Linux I/O Redirection

Redirection can be defined as changing the way from where commands read input to where commands sends output. You can redirect input and output of a command.

LPI Study Materials, LPI Guides, LPI Learning, LPI Tutorial and Material

For redirection, meta characters are used. Redirection can be into a file (shell meta characters are angle brackets '<', '>') or a program ( shell meta characters are pipesymbol '|').

Standard Streams In I/O Redirection


The bash shell has three standard streams in I/O redirection:

◈ standard input (stdin) : The stdin stream is numbered as stdin (0). The bash shell takes input from stdin. By default, keyboard is used as input.
◈ standard output (stdout) : The stdout stream is numbered as stdout (1). The bash shell sends output to stdout. Output goes to display.
◈ standard error (stderr) : The stderr stream is numbered as stderr (2). The bash shell sends error message to stderr. Error message goes to display.

Redirection Into A File


Each stream uses redirection commands. Single bracket '>' or double bracket '>>' can be used to redirect standard output. If the target file doesn't exist, a new file with the same name will be created.

Overwrite

Commands with a single bracket '>' overwrite existing file content.

◈ > : standard output
◈ < : standard input
◈ 2> : standard error

Note: Writing '1>' or '>' and '0<' or '<' is same thing. But for stderr you have to write '2>'.

Syntax:

cat > <fileName> 

Example:

cat > sample.txt 

LPI Study Materials, LPI Guides, LPI Learning, LPI Tutorial and Material

Look at the above snapshot, command "cat > sample.txt" has created 'sample.txt' with content 'a, b, c'. Same file 'sample.txt' is created again with command "cat > sample.txt" and this time it overwrites earlier file content and only displays 'd, e, f '.

Append


Commands with a double bracket '>>' do not overwrite the existing file content.

◈ >> - standard output
◈ << - standard input
◈ 2>> - standard error

Syntax:

cat >> <fileName>  

Example:

cat >> sample.txt  

LPI Study Materials, LPI Guides, LPI Learning, LPI Tutorial and Material

Look at the above snapshot, here again we have created two files with the same name using '>>' in command "cat >> sample.txt". But this time, content doesn't overwrite and everything is displayed.

Redirection Into A Program


Pipe redirects a stream from one program to another. When pipe is used to send standard output of one program to another program, first program's data will not be displayed on the terminal, only the second program's data will be displayed.

Although the functionality of pipe may look similar to that of '>' and '>>' but has a significance difference. Pipe redirects data from one program to another while brackets are only used in redirection of files.

Example:

ls *.txt | cat > txtFile  

LPI Study Materials, LPI Guides, LPI Learning, LPI Tutorial and Material

Look at the above snapshot, command "ls *.txt | cat > txtFile" has put all the '.txt' files into a newly created file 'txtFile'.

Sunday, 22 July 2018

Linux Shell Commands

In Linux, commands are ways or instructions through which you can instruct your system to do some action. Commands are executed in the command line.

Syntax:


command [option] [argument]

There are some commands which don't have any option or don't accept any argument such as 'clear' and 'pwd'.

clear

The 'clear' command clears out all the previous commands and outputs from terminal display.

pwd

The 'pwd' command stands for 'print working directory'. It doesn't accept any option or argument and displays the detail of current working directory.

Types of Commands


External or built-in commands

Built-in commands are internal commands that are built-in the shell. Built-in commands are called from the shell and executed directly within the shell itself. You can list all built-in commands with the help of 'help' and 'compgen -b' command. Some example of built-in commands are 'pwd', 'help', 'type', 'set', 'unset', etc.

External commands are other than built-in commands. These commands are programs which have their own binary and located in the filesystem. These are the commands that your system offer and are totally shell independent. Mostly these commands reside in /bin, /sbin, /usr/sbin.

type command


Linux 'type' command tell us whether a command given to the shell is a built-in or external command.

Syntax:

type <command>

Example:

type pwd
type cd
type man
type cat
type file

Linux Shell Commands, LPI Study Materials, LPI Guides, LPI Learning, LPI Certification

Look at above snapshot, commands like 'pwd' and 'cd' are built-in commands while commands 'man', 'cat', and 'file' are external commands.

Linux 'type' command also tells whether a command is aliased or not.

Example:

type ls  

Linux Shell Commands, LPI Study Materials, LPI Guides, LPI Learning, LPI Certification

Look at the above snapshot, 'type' command shows that 'ls' is an aliased command.

type -a


The 'type -a' option tells about all type of command whether it is built-in, external, or aliased. Some commands are both external and built-in commands. But built-in command will always takes priority until and unless path of external command is mentioned.

Syntax:

type -a <command>  

Example:

type -a echo  

Linux Shell Commands, LPI Study Materials, LPI Guides, LPI Learning, LPI Certification

Look at the above snapshot, 'echo' command is internal as well as external. To use external 'echo' command, path "/bin/echo" is mentioned.

which


Linux 'which' command locates the path of a command.

Syntax:

which <command1> <command2> <command3>....  

Example:

which ls pwd rmdir mkdir cp cd file man  

Linux Shell Commands, LPI Study Materials, LPI Guides, LPI Learning, LPI Certification

Look at the above snapshot, except 'cd' command, all other commands are external commands because bash has displayed their external path.

Thursday, 19 July 2018

Unix / Linux - File Permission / Access Modes

LPI Study Materials, LPI Guides, LPI Certifications, LPI Learning, LPI Tutorials and Materials

We will discuss in detail about file permission and access modes in Unix. File ownership is an important component of Unix that provides a secure method for storing files. Every file in Unix has the following attributes.

◈ Owner permissions − The owner's permissions determine what actions the owner of the file can perform on the file.

◈ Group permissions − The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.

◈ Other (world) permissions − The permissions for others indicate what action all other users can perform on the file.

The Permission Indicators


While using ls -l command, it displays various information related to file permission as follows.

$ls -l /home/amrood
-rwxr-xr--  1 amrood   users 1024  Nov 2 00:10  myfile
drwxr-xr--- 1 amrood   users 1024  Nov 2 00:10  mydir

Here, the first column represents different access modes, i.e., the permission associated with a file or a directory.

The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: read (r), write (w), execute (x)

◈ The first three characters (2-4) represent the permissions for the file's owner. For example, -rwxr-xr-- represents that the owner has read (r), write (w) and execute (x) permission.

◈ The second group of three characters (5-7) consists of the permissions for the group to which the file belongs. For example, -rwxr-xr-- represents that the group has read (r) and execute (x) permission, but no write permission.

◈ The last group of three characters (8-10) represents the permissions for everyone else. For example, -rwxr-xr-- represents that there is read (r) only permission.

File Access Modes


The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions, which have been described below.

Read

Grants the capability to read, i.e., view the contents of the file.

Write

Grants the capability to modify, or remove the content of the file.

Execute

User with execute permissions can run a file as a program.

Directory Access Modes


Directory access modes are listed and organized in the same manner as any other file. There are a few differences that need to be mentioned.

Read

Access to a directory means that the user can read the contents. The user can look at the filenames inside the directory.

Write

Access means that the user can add or delete files from the directory.

Execute

Executing a directory doesn't really make sense, so think of this as a traverse permission.

A user must have execute access to the bin directory in order to execute the ls or the cd command.

Changing Permissions


To change the file or the directory permissions, you use the chmod (change mode) command. There are two ways to use chmod — the symbolic mode and the absolute mode.

Using chmod in Symbolic Mode

The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.

No Chmod operator & Description
1  +
Adds the designated permission(s) to a file or directory. 
2  -
Removes the designated permission(s) from a file or directory. 
3  =
Sets the designated permission(s).

Here's an example using testfile. Running ls -1 on the testfile shows that the file's permissions are as follows.

$ls -l testfile
-rwxrwxr--  1 amrood   users 1024  Nov 2 00:10  testfile

Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes.

$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod g = rx testfile
$ls -l testfile
-rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile

Here's how you can combine these commands on a single line.

$chmod o+wx,u-x,g = rx testfile
$ls -l testfile
-rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile

Using chmod with Absolute Permissions


The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.

Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.

Number Octal Permission Representation  Ref 
0  No permission  --- 
1  Execute permission  --x 
2  Write permission  -w- 
3  Execute and write permission: 1 (execute) + 2 (write) = 3  -wx 
4  Read permission  r-- 
5  Read and execute permission: 4 (read) + 1 (execute) = 5  r-x 
6  Read and write permission: 4 (read) + 2 (write) = 6  rw- 
7  All permissions: 4 (read) + 2 (write) + 1 (execute) = 7  rwx 

Here's an example using the testfile. Running ls -1 on the testfile shows that the file's permissions are as follows.

$ls -l testfile
-rwxrwxr--  1 amrood   users 1024  Nov 2 00:10  testfile

Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes.

$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod 043 testfile
$ls -l testfile
----r---wx  1 amrood   users 1024  Nov 2 00:10  testfile

Changing Owners and Groups


While creating an account on Unix, it assigns a owner ID and a group ID to each user. All the permissions mentioned above are also assigned based on the Owner and the Groups.

Two commands are available to change the owner and the group of files.

◈ chown − The chown command stands for "change owner" and is used to change the owner of a file.

◈ chgrp − The chgrp command stands for "change group" and is used to change the group of a file.

Changing Ownership


The chown command changes the ownership of a file. The basic syntax is as follows.

$ chown user filelist

The value of the user can be either the name of a user on the system or the user id (uid) of a user on the system.

The following example will help you understand the concept.

$ chown amrood testfile
$

Changes the owner of the given file to the user amrood.

NOTE − The super user, root, has the unrestricted capability to change the ownership of any file but normal users can change the ownership of only those files that they own.

Changing Group Ownership


The chgrp command changes the group ownership of a file. The basic syntax is as follows.

$ chgrp group filelist

The value of group can be the name of a group on the system or the group ID (GID) of a group on the system.

Following example helps you understand the concept.

$ chgrp special testfile
$

Changes the group of the given file to special group.

SUID and SGID File Permission


Often when a command is executed, it will have to be executed with special privileges in order to accomplish its task.

As an example, when you change your password with the passwd command, your new password is stored in the file /etc/shadow.

As a regular user, you do not have read or write access to this file for security reasons, but when you change your password, you need to have the write permission to this file. This means that the passwd program has to give you additional permissions so that you can write to the file /etc/shadow.

Additional permissions are given to programs via a mechanism known as the Set User ID (SUID) and Set Group ID (SGID) bits.

When you execute a program that has the SUID bit enabled, you inherit the permissions of that program's owner. Programs that do not have the SUID bit set are run with the permissions of the user who started the program.

This is the case with SGID as well. Normally, programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.

The SUID and SGID bits will appear as the letter "s" if the permission is available. The SUID "s" bit will be located in the permission bits where the owners’ execute permission normally resides.

For example, the command.

$ ls -l /usr/bin/passwd
-r-sr-xr-x  1   root   bin  19031 Feb 7 13:47  /usr/bin/passwd*
$

Shows that the SUID bit is set and that the command is owned by the root. A capital letter S in the execute position instead of a lowercase s indicates that the execute bit is not set.

If the sticky bit is enabled on the directory, files can only be removed if you are one of the following users.

◈ The owner of the sticky directory
◈ The owner of the file being removed
◈ The super user, root

To set the SUID and SGID bits for any directory try the following command.

$ chmod ug+s dirname
$ ls -l
drwsr-sr-x 2 root root  4096 Jun 19 06:45 dirname
$

Wednesday, 18 July 2018

Split Command Examples in Unix / Linux

The Split command in unix or linux operating system splits a file into many pieces (multiple files). We can split a file based on the number of lines or bytes. We will see how to use the split command with an example.

Split Command, Unix Command, Linux Command, LPI Study Materials

As an example, let’s take the below text file as the source file which we want to split:

> cat textfile
unix linux os
windows mac os
linux environment

There are three lines in that file and the size of the file is 47 bytes.

Split Command Examples:


1. Splitting file on number of lines.


The Split command has an option -l to split the file based on the number of lines. Let say i want to split the text file with number of lines in each file as 2. The split command for this is

split -l2 textfile

The new files created are xaa and xab. Always the newly created (partitioned) file names start with x. We will see the contents of these files by doing a cat operation.

> cat xaa
unix linux os
windows mac os

> cat xab
linux environment

As there only three lines in the source file we got only one line in the last created file.

2. Splitting file on the number of bytes


We can use the -b option to specify the number of bytes that each partitioned file should contains. As an example we will split the source files on 10 bytes as

split -b10 textfile

The files created are xaa, xab, xac, xad, xae. The first four files contain 10 bytes and the last file contains 7 bytes as the source file size is 47 bytes.

3. Changing the newly created file names from character sequences to numeric sequences.


So far we have seen that the newly created file names are created in character sequences like xaa, Xab and so on. We can change this to numeric sequence by using the -d option as

split -l2 -d textfile

The names of the new files created are x00 and x01.

4. Changing the number of digits in the sequence of filenames.


In the above example, you can observe that the sequences have two digits (00 and 01) in the file names. You can change the number of digits in the sequence by using the -a option as

split -l2 -d -a3 textfile

Now the files created are x000 and x001