- A file is a place to store data: a possibly-empty sequence of bytes
- A directory is a collection of files and other directories
- Directories are organized in a hierarchy, with the root directory at the top
- The root directory is referred to as
/
Nguyen Hai Chau
Vietnam National University
/
/
, for example:/bin/ls
/usr/share/dict/words
/home/jeff/recipe
/
(absolute) or from some 'current' directoryExtension | File type |
---|---|
.txt | Text file |
.gif | Graphics Interchange Format image |
.jpg | Joint Photographic Experts Group image |
.mp3 | MPEG-2 Layer 3 audio |
.gz | Compressed file |
.tar | Unix 'tape archive' file |
.tar.gz, .tgz | Compressed archive file |
pushd
command takes you to another directory, like cd
.
But also saves the current directory, so that you can go back later$ pushd ˜fred
$ cd Work
$ ls
...
$ popd
popd
takes you back to the directory where you last did pushd
dirs
will list the directories you can pop back toCtrl+D
*
to match any part of a filename:$ ls *.txt
accounts.txt
letter.txt
report.txt
*
produces the names of all files in the current directory?
matches exactly one character:$ rm -v data.?
removing data.1
removing data.2
removing data.3
cp
Syntax: cp [options] source-file destination-file
Copy multiple files into a directory: cp files directory
Common options:
-f
, force overwriting of destination files-i
, interactively prompt before overwriting files-a
, archive, copy the contents of directories recursivelycp
/etc/smb.conf
to the current directory:$ cp /etc/smb.conf .
work
, and call it work-backup
:$ cp -a work work-backup
$ cp *.gif *.jpeg images/
mv
mv
can rename files or directories, or move them to different directories-f
, force overwrite, even if target already exists-i
, ask user interactively before overwriting filespoetry.txt
to poems.txt
:$ mv poetry.txt poems.txt
$ mv * ˜/old-stuff/
rm
rm
deletes ('removes') the specified filesroot
!-f
, delete write-protected files without prompting-i
, interactive — ask the user before deleting files-r
, recursively delete files and directories/tmp
, without prompting to delete each file:$ rm -rf /tmp/*
$ rm ./-filename
$ rm -- -filename
$ rm -i ./name-with-funny-characters*
./
forces it to be in the current directory-i
option to rm
makes sure that you won't delete anything else by accidentmkdir
mkdir directory-names
-p
, create intervening parent directories if they don't already exist-m
mode, set the access permissions to modemystuff
in your home directory with permissions so that
only you can write, but eveyone can read it:$ mkdir -m 755 ˜/mystuff
/tmp
using one command with three subdirectories called one
, two
and three
:$ mkdir -p /tmp/one/two/three
rmdir
rmdir
deletes empty directories, so the files inside must be deleted firstimages
directory:$ rm images/*
$ rmdir images
rm -r
directory, note: dangerous command-p
option to rmdir
removes the complete path, if there are no other files and directories in
it. These commands are equivalent:$ rmdir -p a/b/c
$ rmdir a/b/c a/b
file
command will try to identify the type of a file:$ file /bin/bash
/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1,
dynamically linked (uses shared libs), stripped
$ file /usr/bin/zless
/usr/bin/zless: Bourne shell script text
$ file /etc/passwd
/etc/passwd: ASCII text
touch
-a
, change only the access time-m
, change only the modification time-t [YYYY]MMDDhhmm[.ss]
, set the timestamp of the file to the specified date and
time. GNU touch has a -d
option, which accepts times in a more flexible format$ touch -t 200101201759 homework
cd
to go to your home directory, and create a new directory there called dog
.cat
, and another within that called mouse
.rm -r
, what is the point of using rmdir
for empty directories?dog/cat/mouse
directory structure with a single command./etc/passwd
to your home directory, and then use cat
to see what's in it.mv
command./bin
into it.touch
command can be used to create new empty files.
Try that now, picking a name for the new file:$ touch baked-beans
b. Get details about the file using the ls
command:
$ ls -l baked-beans
touch
?