Creating, Moving and Deleting

«« Previous
Next »»

If you are ready we can start to look at some of the extensive command line tools within the Linux arsenal. In particular we will concentrate on those that will allow us to manage files and directories. You may not have guessed from the name but did you know that you can create a directory with mkdir. Let’s live the life of Creating Moving Deleting Files.

Files in Linux


To begin with, lets remind ourselves that, in Linux everything is a file and file names are case-sensitive. We can have files called test, Test and TEST all in the same directory as the names are NOT the same, as they are in different case! Talking of directories, a directory is a special type of file, as is a symbolic link. We can use the command file to identify the type of file that we are looking at. We saw this in the previous objective, 2.3 of Linux Essentials.

Listing Files with ls


Additionally, ls  can help with identifying the file type. Consider ls with the –color option enabled:

ls --color=auto

The above command is often turned on by default with ls by means of an alias. When you type the command ls, the alias is found in RAM before the command. Either way, the color option will display files, directories and links in different colors as can be seen with the following screen-shot.

LPI Tutorials and Materials, LPI Guides, LPI Learning

Another option you may find useful with ls if you do not have a terminal that supports color is the -F option

ls -F

The above option -F is a little redundant with the color option so perhaps is less used. However with this option enabled directories are listed with a trailing /, executable files with a trailing * , named pipes with a vertical bar, etc. This can be seen in the the following screenshot but also remember that the color option is turned on by default making the -F option not so necessary:

LPI Tutorials and Materials, LPI Guides, LPI Learning

Listing a Directory


If we want a long listing of a directory rather than the contents of that directory we can use the command:

ls -ld /etc

This will provide a long listing of the directory /etc rather than a long listing of all of the files within /etc as seen in the following screenshot:

LPI Tutorials and Materials, LPI Guides, LPI Learning

Pipes


The command ls does not stop here in identifying file types, a long listing with ls -l will identify the file type as well. Is there no end to the ways we can identify Linux files! The very first character before the permissions is an indicator of the file type:

◉ – Indicates a regular file
◉ d Indicated a directory
◉ l Indicates a symbolic link
◉ c Indicates a character device, a terminal
◉ b Indicates a block device, a disk drive
◉ s Indicates a socket, network connection
◉ p Indicates a named pipe – see not below on pipes

Note: Pipes are communication processes between applications, the simplest form of piping is with unnamed pipes. See Linux Essentials topic 3.2 for more details but briefly: cat file | less is an example of an unnamed pipe. The output of cat is sent through to the input of less. A named pipe is similar but we have a file of type pipe that is used as the intermediary:

1. Make the pipe file with:

 mkfifo mypipe

2. From one terminal type the following command from the same directory that the named pipe was created in:

 ls -l > mypipe

3. From another terminal making sure you are the same directory as the named pipe and then issue the command:

 cat < mypipe

There is no requirement to be in the same directory as the named pipe but if you are not, then just use the full path to the pipe file.

The first character of each line in the output of ls -l identifies the file type as can be seen from the following screenshot:

LPI Tutorials and Materials, LPI Guides, LPI Learning

Video on named pipes:


Using mkdir


Commonly used in Linux is mkdir, the command to create directories. Really important when creatingf moving delting filesd. I often find people are surprised that the -m option is there and did not know you can create the directory and set the permissions in one.

◉ -v : provides positive feedback that the directory as created
◉ -m : Sets the permissions of the directory, don’t forget the sticky bit that ensures users can delete only the files they own and the group ID bit when set on a directory can control file group ownership . See objective 5.3 of Linux Essentials for more details on Linux file permissions.
◉ -p : creates the parent directory if required
◉ -Z : set the SELinux context

The mkdir video:


Quoting


If we are creating a directory or a regular file we may need to quote the filename. For example is we have a space in the name Linux will create two directories:

mkdir new dir

The above command create two directories one name new and one named dir . We have three types a quoting mechanisms that we could use the help in this and other situations:

◉ double quotes
◉ single quotes
◉ and the backslash

In that way these three code snippets would resolve the situation:

mkdir “new dir”
mkdir 'new dir'
mkdir new\ dir

They do have slightly different purposes and they are described below:

The double quote


The double quotes will protect most characters from any shell special meaning. So in the example the quotes protect the space from being the argument separator.

The single quotes


The single quotes work in much the same way as the double quote except that single quotes protect all characters from the shell. This means that should we want to include a variable within the quotes the contents of the variable would not be expanded and the directory name would be named after the variable name. Compare the following:

mkdir “$USER dir”
mkdir '$USER dir'

With double quotes the directory created would be andrew dir .

With single quotes the directory created would be $USER dir .

Backslash


Finally, we have the backslash. This escapes the character immediately following the slash. Only that single character is protected. So as before with a variable in the directory name as well as the space we could use the following code the create the andrew dir.

mkdir $USER\ dir

rmdir


If mkdir makes a directory then its antithesis is rmdir  which can remove a directory. However, it can only remove empty directories.

rmdir /home/user1/test

In the above command the test directory will only be deleted if it si empty.

In you need to delete a directory and its content then you may use the command rm :

rm -rf /home/user1/test

The above command will delete the test directory and its content from user1’s home directory.

Globbing

Globbing is a glorious word, isn’t it? Just take the time to say it a few times. When you are ready we will explain what it does. Ok…. ? Globbing provides techniques to collectively groups files by using elements in their names as the criteria. Using different globbing techniques we can display different collections of files :

◉ ls *.py : show files with the extension py
◉ ls *.[!p] : show files where the extension does not start with a p
◉ ls ??.* : show files where the name starts with two characters followed by a dot and then any characters

The main characters * and ?, their meaning is expanded below:

◉ * : refers to zero or more instances of any characters
◉ ? : refers to exactly a single character

The Copy Command or cp


Again very important when Creating Moving Deleting Files is cp. To copy files we can use the command cp. In copying a file the original file remains intact and new copy is made in the target directory. Using the -R option we can recursively copy which is including files from subdirectories. Using the -i option, for interactive, we will be prompted before files are copied. Other options include:

◉ -a : Is probably well known as the archive option, maintaining ownership of the files, but did you know it also turned on the recursion option so that sub-directories are included.
◉ -b : If a target file is overwritten then back that file up and append a ~ to the end of the name.
◉ -n : no-clobber, do not overwrite target file
◉ -u : update the target file if source file is newer
◉ -s : create symbolic links as targets

The cp video:


Move with mv


Using mv(/bin/mv) we can move or rename files. The following command renames the files as the file has not been placed in another directory. The file is renamed from file1 to the new name file1.txt.

mv file1 file1.txt

If we used the following command then the file is moved from /data to the directory /salesdata, as there is now new basename to the file we leave that blank in the second argument:

mv /data/file /salesdata/
If the move is on the same hard drive then now new data file is created. A move on the same disk partition amounts only to a change in the file’s meta-data.

touch


We can use the command touch to create empty files, but using on an exiting file we can change the date stamps with touch -a or touch -m.

rm


Finally we look at rm(/bin/rm). This is used to delete files and we have already seen that we can delete a directory and its content with rm -rf used against the target directory. Take great care when logged in as root, the Linux super-user, rm -rf can delete all files on your system.


«« Previous
Next »»

0 comments:

Post a Comment