Posts

Showing posts from February, 2019

Filtering Input

Grep ( The Weapon) The  grep  command is a text filter that will search input and return lines, which contain a match to a given pattern. grep [ OPTIONS ] PATTERN [ FILE ] Follow Along Use the following command to switch to the  Documents  directory: sysadmin@localhost : ~ $ cd ~/Documents If the example below fails, repeat the example from Section 11: sysadmin@localhost : ~/Documents $ cp /etc/passwd . For example, the  passwd  file we previously copied into the  Documents  directory contains the details of special system accounts and user accounts on the system. This file can be very large, however the  grep command can be used filter out information about a specific user, like the  sysadmin  user. Use  sysadmin  as the pattern argument and  passwd  as the file argument: sysadmin@localhost : ~/Documents $ grep sysadmin passwd sysadmin :x:1001:1001:System Admi...

Removing Files

The  rm  command is used to delete files and directories. It is important to keep in mind that deleted files and directories do not go into a "trash can" as with desktop oriented operating systems. When a file is deleted with the  rm  command, it is almost always permanently gone. rm [ OPTIONS ] FILE Follow Along Use the following command to switch to the  Documents  directory: sysadmin@localhost : ~ $ cd ~/Documents Without any options, the  rm  command is typically used to remove regular files: sysadmin@localhost : ~/Documents $ rm linux.txt sysadmin@localhost : ~/Documents $ ls linux.txt ls: cannot access linux.txt: No such file or directory The  rm  command will ignore directories that it's asked to remove; to delete a directory, use the recursive option, either the  -r  or  -R  options. Just be careful since this will delete all files and all subdirectories: sysadmin@localhost : ~/Documents $...

Copying Files ( Part 2 )

The  dd  command is a utility for copying files or entire partitions at the bit level. dd [ OPTIONS ] OPERAND This command has several useful features, including: It can be used to clone or delete (wipe) entire disks or partitions. It can be used to copy raw data to removable devices, such as USB drives and CDROMs. It can backup and restore the MBR (Master Boot Record). It can be used to create a file of a specific size that is filled with binary zeros, which can then be used as a swap file (virtual memory). Let's examine the following example, the  dd  command creates a file named  /tmp/swapex  with 50 blocks of zeros that are one megabyte in size: sysadmin@localhost : ~ $ dd if=/dev/zero of=/tmp/swapex bs=1M count=50 50+0 records in 50+0 records out 52428800 bytes (52 MB) copied, 0.825745 s, 635 MB/s The  dd  command uses special arguments to specify how it will work. The following illustrates some of the more commonly used arg...

Copying Files ( Part 1 )

Creating copies of files can be useful for numerous reasons: If a copy of a file is created before changes are made, then it is possible to revert back to the original. It can be used to transfer a file to removable media devices. A copy of an existing document can be used as a template for a new document. cp [ OPTIONS ] SOURCE DESTINATION Follow Along Use the following command to switch to the  Documents  directory: sysadmin@localhost : ~ $ cd ~/Documents ​⁠​‌‌⁠⁠⁠⁠​ The  cp  command is used to copy files. Similar to the  mv  command it requires at least two arguments: a source and a destination. For example, to copy the  /etc/passwd  file to the current directory, use the following command: sysadmin@localhost : ~/Documents $ cp /etc/passwd . Note The second argument is the  .  character. Recall from the Changing Directories section that is a shortcut which represents the...

Moving Files

The  mv  command is used to move a file from one location in the filesystem to another. mv SOURCE DESTINATION The  mv  command requires at least two arguments. The first argument is the source, a path to the file to be moved. The second argument is the destination, a path to where the file will be moved to. The files to be copied are sometimes referred to as the source and the place to where the copies are placed in is called the destination. Follow Along Use the following command to switch to the  Documents  directory: sysadmin@localhost : ~ $ cd ~/Documents To move the  people.csv  file into the  Work directory, use the filename as the source, and the directory name as the destination: sysadmin@localhost : ~/Documents $ mv people.csv Work If a file is moved from one directory to another and without specifying a new name for the file, it will retain its original name. The move above can be co...

Changing File Ownership

Initially, the owner of a file is the user who creates it. The  chown  command is used to change the ownership of files and directories. Changing the user owner require administrative access, a regular user cannot use this command to change the user owner of a file, even to give the ownership of one of their own files to another user. However, the  chown  command also permits changing group ownership, which can be accomplished by either root or the owner of the file. To change the user owner of a file, the following syntax can be used. The first argument  [OWNER]  specifies which user is to be the new owner. The second argument  FILE  specifies of which file the ownership is changing. chown [OPTIONS] [OWNER] FILE Follow Along Use the following command to switch to the  Documents  directory: sysadmin@localhost : ~ $ cd ~/Documents Currently all the files in the  Documents  directory are owned by the  sysadmin...

Changing File Permissions

The  chmod  command is used to change the permissions of a file or directory. Only the root user or the user who owns the file is able to change the permissions of a file. Consider This! Why is the command named  chmod  instead of  chperm ? Permissions used to be referred to as  modes of access , so the command  chmod  really means  change the modes of access . There are two techniques of changing permissions with the  chmod  command:  symbolic  and  octal . The symbolic method is good for changing one set of permissions at a time. The octal or numeric method requires knowledge of the octal value of each of the permissions and requires all three sets of permissions (user, group, other) to be specified every time. For the sake of simplicity, only the symbolic method will be covered. (at least for now) Follow Along Use the following command to switch to the  Documents  directory: sysadmin@local...