Working with Files/Directories
Below are some simple examples of basic Linux commands for accessing and changing your files and directories. For all of these commands, you can run man command_name to see the manual page (system documentation) on the command, including an explanation of the available command line options.
- Show the current “full path”, the directory you are working in with its parent and all levels of grandparents up to the root directory (/):
scc1$ pwd /usr2/collab/adftest2 - Create a file:
scc1$ touch myfileThis command will create a blank file namedmyfile. You will also want to be able to use a text editor to create or modify an existing text file. A simple graphical editor to use isgedit, but more complex editors likeemacsandviare also available by default. - Create a directory:
scc1$ mkdir newdir - List items, including files and directories, in a directory:
scc1$ ls newdir newdirThere are many options to the
lscommand such asls -lto list the files in a directory in “long” (verbose) format such as:scc1$ ls -l newdir total 0 drwxr-xr-x 3 adftest2 adftest 512 Oct 28 16:03 newdirThe letters on the left (“drwxr-xr-x”) indicate the “permissions” of this file/directory. The “d” indicates that it is a directory. The next 9 characters indicate that it is “readable”, “writable”, and “executable” by you and “readable” and “executable” by both the members of your project group and the world. The commands you can use to change these “permissions” are chmod and umask.
- Move and/or rename a file:
scc1$ mv myfile newdir/newfilenameThis command will move the file
myfileinto the directorynewdirand simultaneously rename it tonewfilename. - Move to a different directory:
scc1$ cd newdirMove to the newly created
newdirdirectory. You can also specify a “full path” (a path that starts with a /) such ascd /usr/local/bin. Typing justcd(orcd .) by itself will always take you to your home directory. - Move to the parent (next higher level) directory of the one you are in:
scc1$ cd ..This takes you up one level in the SCC directory structure.
- Copy a file:
scc1$ cp newfilename filecopyThis command will make a copy of
newfilenameand name itfilecopy. You can copy entire directories by using the-r(recursive) option. - Delete a file:
scc1$ rm filecopy rm: remove regular empty file `filecopy'? yBy default you will be asked to confirm that you want to remove a file by typing y. You could avoid this by using the command line option
-fbut then you must be much more careful. Empty directories are removed using the commandrmdir directory_name. Full directories can be removed by again using the-r(recursive) option torm.
Tar and Compressed Files🔗
There are four main archiving (combining multiple files into one archive file) and compression (reducing the size of a file) tools on Linux systems with associated tools to reverse the process. It is common to both archive a set of files and then compress the archive, such as a file named myarchive.tar.gz
| Archive/ Compression Tool | Unarchive /Uncompression Tool | Archived/ Compressed Filename Extension | Purpose |
|---|---|---|---|
| gzip | gunzip | .gz | Compress a large single file |
| compress | uncompress | .Z | Compress a large single file |
| zip | unzip | .zip | Creating a single compressed file from a group of files |
| tar -c | tar -x | .tar | Creating a single file from a group of files |
Usage of the first two tools (gzip and compress) and their counterparts is straightforward:
scc1$ ls
myfile
scc1$ compress myfile # or replace with gzip
scc1$ ls
myfile.Z
scc1$ uncompress myfile.Z # or replace with gunzip of a .gz file
scc1$ ls
myfile
On Linux systems, tar is used much more commonly for archiving sets of files than zip. However, you may very well come across ZIP archives, often generated on Windows, which you will need to unzip:
scc1$ unzip example.zip
Archive: example.zip
creating: Packet1/
creating: Packet2/
scc1$ ls
Packet1/ Packet2/ example.zip
Using Tar
Tar has many options. Shown below is an example of generating and then expanding a simple archive. Note that if you have a compressed tar file such as myarchive.tar.gz you will generally first want to uncompress it using the appropriate tool above and then untar it.
scc1$ ls mydir
file1 file2
scc1$ tar -cvf mydir.tar mydir/ # Generate the archive mydir.tar from the directory mydir and all of its contents.
mydir/
mydir/file1
mydir/file2
scc1$ rm -r mydir # Remove the original directory for now
scc1$ ls
mydir.tar
scc1$ tar -tvf mydir.tar # Look at what is in the archive file.
drwx------ aarondf/scv 0 2014-04-30 13:26 mydir/
-rw------- aarondf/scv 8 2014-04-30 13:25 mydir/file1
-rw------- aarondf/scv 10 2014-04-30 13:26 mydir/file2
scc1$ tar -xvf mydir.tar # Expand the archive file in my current directory.
mydir/
mydir/file1
mydir/file2
scc14 ls *
mydir:
file1 file2
mydir.tar
