Skip to main content

Unix Fundamentals: File Manipulation

Being an effective user of Unix likely means modifying, moving, renaming, searching for and deleting files and directories.

NOTE: Windows has folders. Unix has directories. Functionally they are the same. Only the icons are different.

Unix is meant to be a shared yet secure operating system. The security implementation for unix resolves around user, group and global permissions. Each file and directory has permissions attributes for owner, group, and global users.

Types of Permissions
Owner
Access rights for the owner of the file or directory
Group
Access rights for the group owner of the file
Global
Access rights for everyone else

Using the ‘ls –l’ command, we can see what the file permissions are for any given file or folder, like so:









Let’s take a closer look at the access rights.
-                       this is not a directory
r w x                owner access is read and write, and execute
r - x                  members of the dba group have read and execute, not write
r - -                  everyone else has read only access

The ‘chmod’ command allows you modify permissions of a file; but a little math is required. Each attribute is assigned a value. The sum of each value is the permissions granted, see table below.

Attribute Values
Read
4
Write
2
Execute
1

The sum must be calculated for user, group and global permissions. Now, we are ready for chmod.

CHMOD EXAMPLES
  • chmod 777 file1
    • grants read,write,execute to users, groups and global users
    • generally this is a bad idea
  • chmod 077 file1
    • removes access to the owner of the file and grants read,write,execute to group members and everyone else.
    • Generally, this is an even worse idea
  • chmod 755 file1
    • grants read,write,execute on file to owner, and read and execute to everyone else.
  • ‘man chmod’ for more examples



COMMANDS

  • ls –lthr
    • displays the files and directories in the current directory with owner and group information, sized in human readable terms and sorted in reverse chronological order by modified date.
    • ‘man ls’ for more display options
  • mv file1 file2
    • renames file1 to file2
  • cp file1 file2
    • copies file1 and names the copy file2
  • rm file1
    • deletes file1
  • rm –r dirname
    • deletes directories and subdirectories
    • Imagine a world without a recycle bin, welcome to that world.
  • find /path/ file1
    • recursively searches the given path for file1
    • find / file1 will search the entire system for file1, it will take some time.
  • touch file3
    • creates an empty file called file3
  • grep something file1
    • search for the word ‘something’ in file1
    • grep will return each line with ‘something’ in it
    • grep is case-sensitive, unless you tell it otherwise
    • ‘man grep’ for more options
  • tail file1
    • look at the last 10 lines of file1
    • ‘man tail’ for more options
  • head file1
    • look at the first 10 lines of file1
    • ‘man head’ for more options
  • less file1
    • browse file1 in read-only mode
    • similar to ‘more’ command
    • works well with grep (ie grep something | less)
  • mkdir
    • create new directory


Comments