460 likes | 668 Views
Barry Britt, System Support Group Department of Computer Science Iowa State University. Unix Files and Operations. Statement: In UNIX (Linux), everything is a ________. FILE. mv → Move or Rename a file cp → Copy file to a new location mkdir → Create a new folder
E N D
Barry Britt, System Support Group Department of Computer Science Iowa State University Unix Files and Operations
Statement: In UNIX (Linux), everything is a ________. FILE
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 File Operations
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
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, cont
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 mv - concerns
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
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>' cp - concerns
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 mkdir / rmdir
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
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
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) rm – concerns, cont
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
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 cat, cont.
(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” Unix Files
stat <filename> Display file information Size Disk Blocks # Links Inode number Privileges/permissions (coming soon...) Access/Modify/Change times stat
Has GREAT information about the file. All of the file information (except file name) is contained in the inode. What to remember? Links Concept of an 'inode' → pointer to a location on the hard drive stat, cont.
Ln [-s] <source> <link-name> Create a new link (file or directory): Hard link -or- Soft link ln
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
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. Soft Links , cont.
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
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. Hard Links, cont.
So, what if: 1) Create a file 2) Create a hard link to the file Will the hard link be valid? Links
So, what if: 1) Create a file 2) Create a hard link to the file 3) 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? Links, cont.
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
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? Permissions, cont.
- → ordinary file d → subdirectory l → symbolic link s → socket (CS352) b → block device (disk drive) c → character device (tty, keyboard, mouse) File/Device Bits
r → 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' User 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). Group 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. Other Mode Bits
drwxr-xr-x 2 bbrittbbritt .... stat Red: File/Device bit Green: User Mode bits Blue: Group Mode bits Yellow: Other Mode bits Purple: Owner Turquoise: Group ls example, again...
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. Read 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. Write 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. Execute Bit
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? Permission Functions, cont.
chmod: change the mode (permissions) on a file or directory. You can use this executable in two ways. chmod
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 1
Format 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
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, usage 2, cont.
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 chmod with special bits
User: neo Group: 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? Examples
Take the UNIX '/tmp' directory: Needed by all users for program execution: X windows Temporary files so... directory should be mode (drwxrwxrwx). Are there any drawbacks to this???? More Examples
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. A few more programs
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 chown