See ext2 filesystem basics for a quick introduction to files, directories, links, and that sort of stuff, or this might be somewhat confusing.


Listing files

Alright. As you have seen before you can view a file's attributes by typing "ls -li", or "ls -ldi" for directories. For more options to ls see the manual page by typing "man 1 ls".

Copying files

Copying files. You can copy a file like so:

tribbel:~/docs/culture% cp hamlet.all.txt hamlet.txt
tribbel:~/docs/culture% ls -li hamlet.all.txt hamlet.txt
  98700 -rw-r--r--    1 tribbel  staff      200059 May 30 17:41 hamlet.all.txt
  98694 -rw-r--r--    1 tribbel  staff      200059 Sep  6 16:48 hamlet.txt

See? Two files, two inodes, same size, different alteration dates. To copy a directory give the '-r' option to cp. Like so:

tribbel:~/docs% cp -r culture art
tribbel:~/docs% ls -ldi culture art
 238581 drwxr-xr-x    3 tribbel  staff        1024 Sep  6 16:49 art
  98691 drwxr-xr-x    3 tribbel  staff        1024 Sep  6 16:48 culture

There. Two directories, two inodes, same size, same number of subdirectories, different alteration date. See the manual page for cp for more options by typing "man 1 cp".

Change working directory

If you've worked with MS-DOS before, you know the "cd" command. Note that ext2 uses `/' as a delimiter between subdirectories, and not `\' like MS-DOS. To change to my newly created "art" directory we can do:

tribbel:~/docs% cd art
tribbel:~/docs/art%

To get back we can do:

tribbel:~/docs/art% cd ..
tribbel:~/docs%

To go into the music subdirectory, we can do:

tribbel:~/docs% cd art/music
tribbel:~/docs/art/music%

Another special directory to note is `.' this is the same directory as the one we're already in:

tribbel:~/docs/art/music% cd .
tribbel:~/docs/art/music%

(Re)moving files

Now I have duplicate files on my harddrive. I don't like that, because they take up space. So I'm going to remove them. First, I'll remove the duplicate copy of Hamlet.

tribbel:~/docs% cd culture
tribbel:~/docs/culture% rm hamlet.txt
tribbel:~/docs/culture% ls -li hamlet.txt
ls: hamlet.txt: No such file or directory

There. It's gone. Now to remove the "art" directory.

tribbel:~/docs/culture% cd ..
tribbel:~/docs% rm -r art
tribbel:~/docs% ls -ldi art
ls: art: No such file or directory

Now that's gone too. And we have a nice, clean, filesystem. Oops. I wanted to name my "culture" directory "art". This can be accomplished by "moving" the file. Basically nothing changes, but the filename.

tribbel:~/docs% ls -ldi culture
  98691 drwxr-xr-x    3 tribbel  staff        1024 Sep  6 16:56 culture
tribbel:~/docs% mv culture art
tribbel:~/docs% ls -ldi art
  98691 drwxr-xr-x    3 tribbel  staff        1024 Sep  6 16:56 art

Links

You know what? I want both "culture" and "art", and "hamlet.txt" and "hamlet.all.txt". Let's create links. We'll softlink the directory, because hard linking directories is not allowed.

tribbel:~/docs% ls -ldi art
  98691 drwxr-xr-x    3 tribbel  staff        1024 Sep  6 16:56 art
tribbel:~/docs% ln -s art culture
tribbel:~/docs% ls -ldi art culture
  98691 drwxr-xr-x    3 tribbel  staff        1024 Sep  6 16:56 art
 117373 lrwxrwxrwx    1 tribbel  staff           3 Sep  6 17:03 culture -> art

The 'l' in the permission field is used to indicate that this is a link. Now for Hamlet.

tribbel:~/docs% cd culture
tribbel:~/docs/culture% ln hamlet.all.txt hamlet.txt
tribbel:~/docs/culture% ls -li hamlet.all.txt hamlet.txt
  98700 -rw-r--r--    2 tribbel  staff      200059 May 30 17:41 hamlet.all.txt
  98700 -rw-r--r--    2 tribbel  staff      200059 May 30 17:41 hamlet.txt
tribbel:~/docs/culture% ls -li ../art/hamlet.txt
  98700 -rw-r--r--    2 tribbel  staff      200059 May 30 17:41 ../art/hamlet.txt

Quick Reference

MS-DOS          Linux                      Action
---------------------------------------------------------------
copy              cp                copy a file to another file.
move              mv                rename a file.
del               rm                delete a file.
deltree           rm -r             delete a directory.
dir               ls                list file(s).
cd                cd                change working directory.
---               pwd               show working directory.
---               ln                hard link a file.
---               ln -s             soft link a file.

Back to Linux for Monkeys

Log in or registerto write something here or to contact authors.