An important skill to hone is good file management habits.
We will begin today in our home directory.
You can always return to your home directory, no matter where you are in the directory structure, by typing cd with no further directory information.
Step 1.To return to your home directory, type:
cd Enter
Step 2.To check the working directory, view the information preceding the prompt, or type:
pwd Enter
You are now in your own home directory.
Step 3.To change to your own Shared directory, type:
cd Shared Enter
Step 4.Check your current working directory to verify you are in Shared.
Copying and Moving Files
Occasionally, you will want to make a backup copy of a file or move a file to a different directory. These actions can be performed with the copy (cp) and move (mv) commands.
The syntax of these two commands is:
[command] -[options] [path to source] [path to destination]
Copying Files
We can copy our public.txt file using the cp command. We will use the .. shortcut to copy our public.txt file one level up, to our home directory.
Step 1.To copy public.txt file to your home directory, type:
cp public.txt .. Enter
Your file is now copied.
NOTE: If we were in a different directory from the file we wanted to copy, we would have had to specify the directory of the file.
Step 2.To return to home directory, type:
cd Enter
Step 3.To list your home directory, type:
ls Enter
We see the copy of public.txt. We can copy and rename it with one command.
Step 4.To copy your public.txt file into the Shared directory with a new name type:
cp public.txt Shared/mystuff.txt Enter
Step 5.To list the contents of the Shared directory without changing into it, type:
ls Shared Enter
You see the file has been copied to the Shared directory as mystuff.txt.
NOTE: In addition, there is another protocol that is used often called Secure Copy (SCP). SCP is most often used for moving data between different computers, but may be used among users of a single machine, too. SCP encrypts the details of the process for logging in, and the transfer of the data. We will not use this protocol today. However, if you would like to learn more about SCP, at the command prompt, type: man scp Enter.
Renaming Files
The mv command is used the exact same way that cp is used, only mv moves the file instead of copying it. Although it may seem a bit odd, mv is also used to rename files. The idea is that when you move a file into a place (filename) that doesn't already exist, then Unix gives your file that new name.
Step 1.To rename the public.txt file in your home directory to myfile.txt, type:
mv public.txt myfile.txt Enter
Step 2.To review the changes you made, type:
ls Enter
You see the file with its new name, myfile.txt.
Copying a Directory
We can copy a whole directory and all of its subdirectories by using the -R(recursive) option. We'll also rename the directory as we copy it so that our username is its new name.
Step 1.To copy the Shared directory and change its name, type:
cp -R Shared
your usernameEnter
Step 2.To confirm that the new copy of the directory exists, type:
ls -F Enter
You see a new directory with the name, your username/.
Archiving Files
Moving directories and maintaining their structure is much easier if we make an archive of the directory first. An archive takes all of the contents of a directory, including its subdirectories and files, and makes a single file out of them. We can then move that single file to a new location and un-archive it to expand it to its original contents. Both archiving and un-archiving are accomplished with tar, which stands for tape archiving. We are interested in the archiving, not the tape!
We will use the tar command with the -c and -foptions to make an archive of our username directory. The-c option creates the archive and the -foption allows us to specify a name for our archive by putting it as the path to destination argument. If we do not use the -f option, a path to destination won't be specified, and the tar file will be given a default name.
Step 1.To make a tar archive of your directory, type:
tar -cf username.tar username
Enter
Note that we gave our tar file the extension of.tar, so we know it is a tar file just by its name. Let's look at the mode and size of our new file.
Step 2.To see your file permissions and the size of the file, type:
ls -l Enter
You see that your username.tar file only has read and write (-rw-) permissions, because it is a file and not a directory.
Compressing/Uncompressing Files
In addition to archiving many files and directories into a single file, we can compress, or zip, files to make them smaller. Zipped files are easier and faster to transfer to other machines. Like the tar command, we will need to zip the file, and then unzip it after we move it somewhere. Unlike tar, each of these two actions requires a different command: gzip and gunzip.
The gzip command runs a program which can compress files. We will compress the tar file that we just created. This is a common practice when moving a whole directory structure to another machine, that is:
archive-> compress -> move to another machine -> uncompress -> unarchive
Step 1.To compress the tar archive we just created, type:
gzip username.tar Enter
The command created a zipped file called username.tar.gz. Let's check the permissions on our compressed archive to be sure others have access to the file.
Step 2.To check the permissions on this file, type:
ls -l Enter
The default permissions give others access to the compressed archive file.
Copying a File Using Its Path
We can copy our partner's compressed tar file without moving into their home directory if we use the cp command with the full path of the file. Remember to use the appropriate letter case!
Step 1.To copy your partner's compressed tar file to your own home directory without changing into his/her directory, type:
cp ~partner's username /partner's username.tar.gz ./ Enter
NOTE: If you are using these materials in self-study mode, use jump059 as your partner's username.
In the above command, the "~" stands for the path to your partner's home directory, and the "./" stands for your current directory.
Step 2.To see your file listing and permissions, type:
ls -l Enter
Unzipping and Unarchiving
Now that we have our partner's file, we need to uncompress it and unarchive it.
Step 1.To uncompress the gzip file, type:
gunzip partner's username.tar.gz Enter
Step 2.To list your directory, type:
ls Enter
Gunzip created an uncompressed file and deleted the original file.
Our file is unzipped, but it is still an archive, so now we need to unarchive it. We will unarchive our tar file by using the tar command with the -x, -f, and -v options. The -xoption means extract, which tells the tar command to reverse the process of archiving. The-foption is the same as before; it says that the next argument in the command is the filename that we want to un-archive. The -vcommand stands for verbose, which tells the tar command to display information about what it is extracting as it is doing it. We also would like to indicate that the files should be unarchived to our www directory. We can do this by adding a -C option after the file name and specifying the name of the directory.
Step 3.To extract the tar file, type:
tar -xvf partner's username.tar
-C www Enter
Step 4.To see the ownership of the tar file and of the extracted contents, type:
ls -l www Enter
You have a new directory called partner's username. Let's look inside of it.
Step 5.To list this new directory, type:
ls -l www/partner's username Enter
The permissions of each file remain the same as what your partner set.
Creating Multiple Directories Simultaneously
When you are creating directories in Unix you can create directories one at a time as we did earlier. However, if you have a bunch of directories that you want to create you can use Unix to create these more efficiently.
A common practice on web pages is to have directories for different type of files. Let's create a flash directory and a css directory inside our www folder.
Step 1.To move into the www directory, type:
cd www Enter
Step 2.To create the directories, type:
mkdir flash css Enter
Step 3.To see your new directories type:
ls Enter
You can see that by putting a space between the directory names we created two directories with one command.
To more efficiently create a directory containing subdirectories you can use the mkdir command with the -p option. The -p option tells mkdir to create the parent directory if it does not already exist. This allows you to create nested directories with one command.
We want to create a temp directory that contains a sub1 directory and the sub1 directory to contain a sub2 directory
Step 4.To create the directory tree, type:
mkdir -p temp/sub1/sub2 Enter
Step 5.To view the temp directory type:
ls Enter
You see temp directory. Let's check that the sub1 directory is inside.
Step 6.To view the sub1 directory, type:
ls temp Enter
Now let's see that the sub2 directory is inside the sub1 directory.
Step 7.To view the sub2 directory, type:
ls temp/sub1 Enter
The two methods can be used together. Let's create an images directory that contains two subdirectories.
Step 8.To create the directories, type:
mkdir -p images/jpeg images/gif Enter
The images directory containing a jpeg and gif directory was created. Let's confirm this.
Step 9.To see the images directory, type:
ls Enter
Step 10.To see the jpeg and gif directories, type:
ls images Enter
You see the jpeg and gif directories.
Step 11.To return to your home directory, type:
cd Enter
Deleting Files and Directories
It is important to manage the disk space that you have in your account. Every user is given a quota, or maximum amount of storage space. Deleting files will free up space in your account.
Let's delete some of our files and directories. We will use the remove directory command, rmdir, to delete our Shared directory,
Step 1.To remove the Shared directory, type:
rmdir Shared Enter
You see an error message telling you that the directory is not empty. By default, you can only remove a directory if it is empty, so first you'll have to delete the contents of Shared before you can remove it.
Step 2.To change to the Shared directory, type:
cd Shared Enter
Step 3.To see the files in the Shared directory, type:
ls Enter
To delete a file, we will use the remove command, rm.
Step 4.To remove the public.txt file, type:
rm public.txt Enter
Depending on the configuration of your host, you might see: rm: remove regular file ‘public.txt'? If so, you must confirm that you want to remove the file.
Step 5.If you are asked to confirm the rm command, type:
y Enter
Step 6.To remove the mystuff.txt file, type:
rm mystuff.txt Enter (if necessary: y Enter)
Step 7.To remove private.txt file, type:
rm private.txt Enter (if necessary: y Enter)
Step 8.To see any files in the Shared directory, type:
ls -a Enter
Now that the contents of Shared have been deleted, you should be able to remove it. You cannot, however, remove a directory when you are in it, so first you'll have to move out of Shared.
Step 9.To return to your home directory, type:
cd Enter
Step 10.To remove the Shared directory, type:
rmdir Shared Enter
Let's delete the tar file that we copied from our partner.
Step 11.To remove the.tar file you copied from your partner, type:
rm partner's username.tar Enter (if necessary:
y Enter)
You can use options with the rm command to remove a directory and all of its contents in one command. The-roption (recursive), tells the computer to take everything inside of the directory and the -f(force) tells it no matter what. Clearly, these options can be dangerous because you can easily delete more than you want to, so use them with caution.
Step 12.To remove a directory and all it's contents, type:
rm -rf your usernameEnter
NOTE: The -f option deletes any items without asking you to confirm your command. Use this option with extreme care.
Managing Multiple Files Simultaneously
It is possible to use many of the commands we have just been using on multiple files simultaneously. That can rapidly speed up your file management.
We will begin by making certain we are in our respective home directories.
Step 1.To make sure you are in your home directory, type:
cd Enter
We will copy an archived file containing many files to our directory, then we will see how to rapidly manage those files in the directory structure we have created.
The file that we want is a compressed tar file called example.tar.gz, and it is located in the www directory of the mercury account named jump059.
Step 2.To get the example.tar.gz file, type:
cp ~jump059/www/example.tar.gz./ Enter
The file is transferred into your own mercury account's home directory.
Step 3.To verify the file transfer, type:
ls Enter
Just as we did before, we need to unzip and untar our file before we can see its contents.
Step 4.To uncompress the file, type:
gunzip example.tar.gz Enter
Step 5.To extract the example tar file, type:
tar -xvf example.tar Enter
Step 6.To see the directory that was created, type:
ls Enter
We now have a new directory called Example. We will use this Example directory and its contents to look at some other techniques for issuing commands, and the power of using wildcards when working with multiple files. First, let's look at what is inside this new directory.
Step 7.To list the example directory, type:
ls Example Enter
You see:
fred.txt image5.gif jane.txt pic4.jpeg pict2.jpg
image3.gif img1.gif john.txt pic5.jpeg
image4.gif img2.gif pic3.jpeg pict1.jpg
Using Wildcards
Wildcards are a very useful feature of Unix. Wildcards allow the substitution of a single character for a group of characters. This is similar to the "~" shortcut we saw earlier. The asterisk (*) is the wildcard that may be substituted for any number of characters. Since we'll be using our newly downloaded files for this exercise, let's change into the directory where they are located.
Step 1.To change into the Example directory, type:
cd Example Enter
Step 2.To list all files ending with.jpeg, type:
ls *.jpeg Enter
You see only the files ending in.jpeg listed.
Step 3.To list all jpeg files, including those with the.jpg extension, type:
ls *.j* Enter
NOTE: Be careful to type the "." before the "j," or it will list all files with a "j" in them!
Step 4.To list all files with the.gif extension, type:
ls *.gif Enter
You see only the files ending in.gif listed.
Step 5.To list all files containing 5 in their name, type:
ls *5.* Enter
Organizing Multiple Files
Let's quickly organize these files. First we'll put the.jpeg files into the jpeg directory we already created (www/images/jpeg), and then we'll delete all of the.gif files. We will use the.. shortcut to help us direct the graphic files into the desired destination directory, since the Example directory we are currently in is at the same level in the account as the www directory (go up one directory level, then drill down into the jpeg directory).
Step 1.To move all jpeg files into the jpeg subdirectory inside the images directory, type:
mv *.j* ../www/images/jpeg Enter
Step 2.To verify the jpeg files have been moved as expected, type:
ls../www/images/jpeg Enter
Step 3.To remove all gif files, type:
rm *.gif Enter
You are prompted to confirm this potentially dangerous action for each file.
Step 4.To confirm the deletion of the gif files, for each file, type:
y Enter
Step 5.To see what your directory now looks like, type:
ls Enter
Your files are now much more organized since similar items are grouped together.
Concatenating Files
The cat command may be used to view the contents of text files (similar to the lesscommand), or to concatenate (join together) several text files into one.
Step 1.To view the contents of john.txt, type:
cat john.txt Enter
You see the address of John Doe.
Step 2.To view the contents of all the text files in our current directory, type:
cat *.txt Enter
You see the text from all the files, with no breaks to mark the beginning or end of the files.
Redirecting Output to a File
The ">" character is used to redirect the output of a command to a file, rather than to the screen.
Step 1.To concatenate all of the text files into one file called all.txt, type:
cat *.txt > all.txt Enter
Step 2.To view the contents of all.txt, type:
less all.txt Enter
You see a file that contains everyone's address. This technique could be used to assemble one large database from several small data files.
Step 3.To end the output of this less command, type:
q
You see the command prompt.