Download - unix Files And Operations - Iowa State Universitybbritt/files/UnixFilesAndOps.pdf · stat → display file/directory information ln → Create hard/soft links mv ... EVERY user is

Transcript

   

Unix Files and Operations

Barry Britt, System Support Group

Department of Computer ScienceIowa State University

   

Statement:In UNIX (Linux), everything is a ________.

FILE

   

File Operations

mv   Move or Rename a file→cp   Copy file to a new location→mkdir   Create a new folder→rmdir   Remove a folder→rm   Remove files and folders→

Others:cat  concatenate files→stat  display file/directory information→ln  Create hard/soft links→

   

mv

Usage 1 – Rename files/directories:

mv <file> <new­filename>mv <directory> <new­directoryname>

Renames file to new­filename or directory to new­directoryname.

This ONLY changes the name, not the file or directory contents.

   

mv, cont

Usage 2 ­ move files/directories:

mv <filename> <directory>mv <directory> <new­subdirectory>

Moves <filename> into <directory> or directory into new­subdirectory

Can rename the file while moving

   

mv ­ concerns

You CAN completely overwrite files using the mv command.

You can prevent this by using some command­line switches:

­b   make a backup of the destination file before →moving

­i   interactive. Prompt me before moving a file→

   

cp

cp <file> <new­filename>Creates <new­filename> and populates with all data from <file>.This makes an entirely new file

cp <filename> <directory>Create new file with path <directory>/<filename>Can rename the file while moving

   

cp ­ concerns

You can STILL completely overwrite files using the cp command.

You can prevent this by using some command­line switches:

­b   make a backup of the destination file before →copying­i   interactive. Prompt me before copying a file→

Copy directories? Use 'cp ­R <dir> <new­dir>'

   

mkdir / rmdir

mkdir <directory>  Make a new directory.→

if the parent directory doesn't exist, mkdir will NOT create the directory

rmdir <directory>  Remove an empty directory.→

If the directory is not empty, rmdir will NOT remove the directory

   

rm

rm <file>Remove a file

rm ­r <list­of­files­and/or­directories>Recursively remove files and directories

By default, rm does NOT remove directories. You have to use 'rm ­r' to remove directories.

   

rm ­ concerns

rm will not prompt you if you want to delete a file unless you use the '­i' switch (interactive mode)

rm will not prompt you at all if you use the '­f' switch (force)

What do you think the following command does:rm ­rf /

   

rm – concerns, cont

Answer:It depends....

On later versions of Linux, rm will NOT remove '/'

On earlier versions of UNIX/Linux, rm doesn't care what directories you remove.

Be sure to check the man page for the default rm options... (­­preserve­root or ­­no­preserve­root)

   

cat

cat <file>Print contents of <file>

cat <file1> <file2> <file3>Print contents of <file1> followed by contents of <file2> followed by contents of <file3>

Very useful for seeing what is in a small file.Can also use shell redirects.

   

cat, cont.

cat file1 file2 > file3 Print contents of file1, followed by contents of file2 and put them into file3.

So, logically, file3 is just a joining of file1 followed by file2

   

Unix Files

(Most) files exist on hard drive.

File system keeps information on where to find all files in what is called an 'inode'.

The file name is completely separate from the 'inode'.

We call all of this “accounting information”

   

stat

stat <filename>Display file information

SizeDisk Blocks# LinksInode numberPrivileges/permissions (coming soon...)Access/Modify/Change times

   

stat, cont.

Has GREAT information about the file.

All of the file information (except file name) is contained in the inode.

What to remember?LinksConcept of an 'inode'   pointer to a location on →the hard drive

   

ln

Ln [­s] <source> <link­name>Create a new link (file or directory):

Hard link ­or­Soft link

   

Soft Links

An indirect pointer to a file's contents.

A soft link does NOT copy data.

A soft link will work anywhere on the hard drive on any partition.

Most common type of link.

Think of it as an alias to a file or directory.

   

Soft Links, cont.

If a soft link is deleted, nothing happens to the file.

If the link source is deleted, the soft link becomes invalid!!!!!

You CAN use soft links for directories.

   

Hard Links

A direct pointer to a file's contents.

A hard link does NOT copy data.

A hard link will NOT work across different disk partitions.

Think of this as a direct reference to the file's inode.

   

Hard Links, cont.

If a hard link is deleted, nothing happens to the file.

If the link source (original file) is deleted, the hard link DOES NOT become invalid.

You can NOT use a hard link for a directory.

   

Links

So, what if:

1) Create a file2) Create a hard link to the file

Will the hard link be valid?

   

Links, cont.

So, what if:

1) Create a file2) Create a hard link to the file3) Create a soft link to the original file

Then, what if we “rm file”?

Will the soft link be valid?

   

Links, cont.

Questions...

When is a file “deleted”?What does “deleting” a file mean?

   

Permissions

Unix is a true multiuser Operating System, so...

 ­ How do you keep people from accessing files that aren't theirs? ­ How do you only allow certain people to access files? ­ If everything in UNIX is a file, how do you give permission to EVERYONE to access a file?

Concept of permissions...

   

Permissions, cont.

EVERY file has a set of permissions.

EVERY user has an identification number (UID)

EVERY group has an identification number (GID)

EVERY user is a member of at least one group.

   

Permissions, cont.

Let's take the output of an 'ls ­l':

drwxr­xr­x 2 bbritt bbritt .... stat­rw­r—r­­ 1 bbritt bbritt .... file

What about the first 10 characters of these lines?

   

File/Device Bits

­  ordinary file→d  subdirectory→l  symbolic link→s  socket (CS352)→b  block device (disk drive)→c  character device (tty, keyboard, mouse)→

   

User mode bitsr  read bit→w  write bit→x  execute bit→

Special:s  SetUID bit→

SetUID bit is in place of the X (execute) bit. It allows the program to be run as that user.

Example   /usr/bin/passwd (has to run as root)→Mode is '­rwsr­xr­x'

   

Group Mode Bits

r  read bit→w  write bit→x  execute bit→

Special:s  SetGID bit→

SetGID bit is in place of the X (execute) bit. On a directory, it ensures that all files/directories created as subdirectories inherit the group (but not necessarily the permissions).

   

Other Mode Bits

Note: Other, meaning everyone else... that is, you are not the owner of the file or in the file's group.

r  read bit→w  write bit→x  execute bit→

Special:t  Sticky bit. Takes the place of the execute bit. →When used, tells the OS that only the owner can delete the file or directory.

   

ls example, again...

drwxr­xr­x 2 bbritt bbritt .... stat

Red: File/Device bitGreen: User Mode bitsBlue: Group Mode bitsYellow: Other Mode bits

Purple: OwnerTurquoise: Group

   

Read Bit

If user has read permission, they can view the contents of a file or directory.

Does not affect whether you can read files in a directory.

   

Write Bit

If not set on a file, you cannot write to the file.

If not set on a directory, you ­ cannot create or delete files or directories within a directory. ­ cannot rename files within or move files within the directory. ­ cannot link files in the directory

Basically, you can't do anything that would involve changing the contents of that directory.

   

Execute Bit

If the x bit is not set on a file, you cannot directly execute that file.

If the execute bit is not set on a directory, you cannot access files within that directory.

   

Permission Functions, cont.

Some interesting combinations:

­rwx­­x­­x : Group and Other can cd into a directory, but not 'ls' the contents.

Group and Other cannot modify the directory

Where would these be useful?

   

chmod

chmod: change the mode (permissions) on a file or directory.

You can use this executable in two ways.

   

chmod, usage 1

Format 1, symbolic mode:<mode>   'r','w','x' for read, write, execute→<who>   'a','u','g','o' for all, user, group, other→

Usage: chmod <who>±<mode>

Example using (­rwxr­x­­­):'chmod o+rwx'

Sets perms to (­rwxr­xrwx)'chmod g­rx'

Sets perms to (­rwx­­­­­­)

   

chmod, usage 2Format 2, octal mode:

That is, (rwx) = (22 + 21 + 20):

 ­ or, for those non­mathematically inclined ­

r  = 4 = 22

w = 2 = 21 just add them up...x = 1 = 20

So, (rwx) = 7 (r—) = 4(rw­) = 6 (­­­) = 0

   

chmod, usage 2, cont.

Example using (­rwxr­x­­­):'chmod 757'

Sets perms to (­rwxr­xrwx)'chmod 700'

Sets perms to (­rwx­­­­­­)'chmod 772'

Sets perms to (­rwxrwx­w­)

   

chmod with special bits

Adding sticky bit:chmod 1777 <dir> will set mode (­rwxrwxrwt)chmod a+rwxt <dir> does the same thing

chmod 2755 <file> will set mode (­rwxr­sr­x)    chmod u+rws,go+rx <file> is the same

chmod 4777 <file> will set mode (­rwsr­xr­x)chmod a+rwx,u+s <file> is the same

   

Examples

User: neoGroup: hackers

Listing:drwx­­x­­x 2 trinity hackers  (size/date) redpill/­rwxr­x­­­ 2 trinity trinity   (size/date) smith

Can neo 'cd' into the 'redpill/' directory?Can neo get a directory listing of the files in 'redpill/'?Can neo read the 'smith' file?

   

More Examples

Take the UNIX '/tmp' directory:

Needed by all users for program execution:X windowsTemporary files

so... directory should be mode (drwxrwxrwx).

Are there any drawbacks to this????

   

A few more programs

chown  change owner→chgrp  change group→

Only 'root' can 'chown' files to a different user.

Only the file owner and root can 'chgrp'.

If file owner, the owner must be in the group you want to 'chgrp' to.

   

chown

chown <user>[:<group>] <file|directory>

­R   recursive, operates on all files and →directories(group is optional, but allowed)

chgrp <group> <file|directory>

­R   recursive, operates on all files and →directories