Linux Tutorial - 5. Learn File Manipulation
Linux Tutorial - 5. Learn File Manipulation
File Manipulation!
The fun begins.
Introduction
We've got some basic foundation stuff out of the way. Now we can start to play around. To begin with we'll learn
to make some files and directories and move them around. Future sections will deal with putting content in them
and more interesting manipulation.
Making a Directory
Linux organises it's file system in a hierarchical way. Over time you'll tend to build up a fair amount of data
(storage capacities are always increasing). It's important that we create a directory structure that will help us
organise that data in a manageable way. I've seen way too many people just dump everything directly at the base
of their home directory and waste a lot of their time trying to find what they are after amongst 100's (or even
1000's) of other files. Develop the habit of organising your stuff into an elegant file structure now and you will
thank yourself for years to come.
Creating a directory is pretty easy. The command we are after is mkdir which is short for Make Directory.
In it's most basic form we can run mkdir supplying only a directory and it will create it for us.
Terminal
1. user@bash: pwd
2. /home/ryan
3. user@bash:
4. user@bash: ls
5. bin Documents public_html
6. user@bash:
7. user@bash: mkdir linuxtutorialwork
8. user@bash:
9. user@bash: ls
10. bin Documents linuxtutorialwork public_html
Line 1 Let's start off by making sure we are where we think we should be. (In the example above I am in my
home directory)
Lines 2 We'll do a quick listing so we know what is already in our directory.
Line 7 Run the command mkdir and create a directory linuxtutorialwork (a nice place to put further work we
do relating to this tutorial just to keep it separate from our other stuff).
Remember that when we supply a directory in the above command we are actually supplying a path. Is the path
we specified relative or absolute? Here are a few more examples of how we can supply a directory to be created
mkdir /home/ryan/foo
mkdir ./blah
mkdir ../dir1
mkdir ~/linuxtutorialwork/dir2
There are a few useful options available for mkdir. Can you remember where we may go to find out the command
line options a particular command supports?
The first one is -p which tells mkdir to make parent directories as needed (demonstration of what that actually
means below). The second one is -v which makes mkdir tell us what it is doing (as you saw in the example above,
it normally does not).
Terminal
1. user@bash: mkdir -p linuxtutorialwork/foo/bar
2. user@bash:
3. user@bash: cd linuxtutorialwork/foo/bar
4. user@bash: pwd
5. /home/ryan/linuxtutorialwork/foo/bar
And now the same command but with the -v option
Terminal
1. user@bash: mkdir -pv linuxtutorialwork/foo/bar
2. mkdir: created directory 'linuxtutorialwork/foo'
3. mkdir: created directory 'linuxtutorialwork/foo/bar'
4. user@bash:
5. user@bash: cd linuxtutorialwork/foo/bar
6. user@bash: pwd
7. /home/ryan/linuxtutorialwork/foo/bar
Removing a Directory
Creating a directory is pretty easy. Removing or deleting a directory is easy too. One thing to note, however, is
that there is no undo when it comes to the command line on Linux (Linux GUI desktop environments typically do
provide an undo feature but the command line does not). Just be careful with what you do. The command to
remove a directory is rmdir, short for remove directory.
Two things to note. Firstly, rmdir supports the -v and -p options similar to mkdir. Secondly, a directory must be
empty before it may be removed (later on we'll see a way to get around this).
Terminal
1. user@bash: rmdir linuxtutorialwork/foo/bar
2. user@bash:
3. user@bash: ls linuxtutorialwork/foo
4.
touch is actually a command we may use to modify the access and modification times on a file (normally not
needed but sometimes when you're testing a system that relies on file access or modification times it can be
useful). I know some students have tried using this command to make it look like their assignment files were not
modified after the due date (there are means to detect this so it never works but points for being creative). What
we are taking advantage of here is that if we touch a file and it does not exist, the command will do us a favor and
automatically create it for us.
Shortcut
Many things in Linux are not done directly but by knowing the behaviour of certain commands and
aspects of the system and using them in creative ways to achieve the desired outcome.
Remember in the introduction we talked about the command line as providing you with a series of
building blocks. You are free to use these building blocks in any way you like but you can really only do
this effectively if you understand how they do their function as well as why.
At the moment the file is blank which is kinda boring but in coming sections we'll look at putting data into files
and extracting data from them.
There are quite a few options available to cp. I'll intoduce one of them further below but it's worth checking out
the man page for cp to see what else is available.
Terminal
1. user@bash: ls
2. example1 foo
3. user@bash:
4. user@bash: cp example1 barney
5. user@bash: ls
6. barney example1 foo
Note that both the source and destination are paths. This means we may refer to them using both absolute and
relative paths. Here are a few examples:
cp /home/ryan/linuxtutorialwork/example2 example3
cp example2 ../../backups
cp example2 ../../backups/example4
cp /home/ryan/linuxtutorialwork/example2 /otherdir/foo/example5
When we use cp the destination can be a path to either a file or directory. If it is to a file (such as examples 1, 3
and 4 above) then it will create a copy of the source but name the copy the filename specified in destination. If we
provide a directory as the destination then it will copy the file into that directory and the copy will have the same
name as the source.
In it's default behaviour cp will only copy a file (there is a way to copy several files in one go but we'll get to that in
section 6. Wildcards). Using the -r option, which stands for recursive, we may copy directories. Recursive means
that we want to look at a directory and all files and directories within it, and for subdirectories, go into them and
do the same thing and keep doing this.
Terminal
1. user@bash: ls
2. barney example1 foo
3. user@bash: cp foo foo2
4. cp: omitting directory 'foo'
5. user@bash: cp -r foo foo2
6. user@bash: ls
7. barney example1 foo foo2
In the above example any files and directories within the directory foo will also be copied to foo2.
Terminal
1. user@bash: ls
2. barney example1 foo foo2
3. user@bash: mkdir backups
4. user@bash: mv foo2 backups/foo3
5. user@bash: mv barney backups/
6. user@bash: ls
7. backups example1 foo
Note that again the source and destination are paths and may be referred to as either absolute or relative paths.
Terminal
1. user@bash: ls
2. backups example1 foo
3. user@bash: mv foo foo3
4. user@bash: ls
5. backups example1 foo3
6. user@bash: cd ..
7. user@bash: mkdir linuxtutorialwork/testdir
8. user@bash: mv linuxtutorialwork/testdir /home/ryan/linuxtutorialwork/fred
9. user@bash: ls linuxtutorialwork
10. backups example1 foo3 fred
Line 3 We renamed the file foo to be foo3 (both paths are relative).
Line 6 We moved into our parent directory. This was done only so in the next line we can illustrate that we
may run commands on files and directories even if we are not currently in the directory they reside in.
Line 8 We renamed the directory testdir to fred (the source path was a relative path and the destination was
an aboslute path).
rm [options] <file>
Terminal
1. user@bash: ls
2. backups example1 foo3 fred
3. user@bash: rm example1
4. user@bash: ls
5. backups foo3 fred
Terminal
1. user@bash: ls
2. backups foo3 fred
3. user@bash: rmdir backups
4. rmdir: failed to remove 'backups': Directory not empty
5. user@bash: rm backups
6. rm: cannot remove 'backups': Is a directory
7. user@bash: rm -r backups
8. user@bash: ls
9. foo3 fred
A good option to use in combination with r is i which stands for interactive. This option will prompt you before
removing each file and directory and give you the option to cancel the command.
Summary
Stuff We Learnt
mkdir
Make Directory - ie. Create a directory.
rmdir
Remove Directory - ie. Delete a directory.
touch
Create a blank file.
cp
Copy - ie. Copy a file or directory.
mv
Move - ie. Move a file or directory (can also be used to rename).
rm
Remove - ie. Delete a file.
Important Concepts
No undo
The Linux command line does not have an undo feature. Perform destructive actions carefully.
Activities
We now have at our disposal, various commands to actually interact with the system. Let's put them into practice.
Have a go at the following:
In that directory, create a series of files and directories (and files and directories in those directories).
Delete one of the directories that has other files and directories in them.
Move back to your home directory and from there copy a file from one of your subdirectories into the initial
directory you created.
Finally, have a look at the existing directories in your home directory. You probably have a Documents,
Downloads, Music and Images directory etc. Think about what other directories may help you keep your
account organised and start setting this up.
Home
(/)
Linux Tutorial
(/linuxtutorial/)
HTML Tutorial
(/html-tutorial/)
Binary Tutorial
(/binary-tutorial/)
CSS Tutorial
(/css-tutorial/)
Regular Expressions
(/regular-expressions-tutorial/)
Programming Challenges
(/programming-challenges/)
Problem Solving
(/problem-solving-skills/)
micro:bit Tutorial
(/microbit-tutorial/)