Linux Commands for Files and Directories PDF
Document Details
Uploaded by SuppleTigerSEye1117
Tags
Summary
This document provides an overview of common Linux commands for managing files and directories. It includes explanations of various commands like creating, deleting, copying, moving, and viewing files, as well as directory management. The document also covers commands for counting lines, words, and characters in files.
Full Transcript
Working with Files and Directories Module 3 Objectives This module introduces commands that allow you to interact with files and directories via command-line mode. Upon completion of this module, you should be able to: Copy files and directories Move and rename files...
Working with Files and Directories Module 3 Objectives This module introduces commands that allow you to interact with files and directories via command-line mode. Upon completion of this module, you should be able to: Copy files and directories Move and rename files and directories Create files and directories Remove files and directories Use symbolic links Working with directories mkdir: create a directory rmdir: remove a directory cd: change a directory pwd: print working directory Working with directories: mkdir Create a single directory Create multiple directories Or a path of directories with -p options Examples: mkdir single mkdir dir1 dir2 mkdir –p grandfather/father/son Working with directories: rmdir rmdir: remove a single directory The directory must be empty Non-empty directories could be removed using rm command with –r option for recursive deletion. Examples: rmdir directory rm –r directory Working with directories: cd Change directory i.e. change from the current place to another one. Examples: Go to the default directory: cd Go to the parent directory: cd.. Go to the previous directory: cd - Go to this directory /usr/share/: cd /usr/share Working with directories: pwd Print Working Directory Display the current place (full path name) Examples: cd /usr/share pwd /usr/share cd.. pwd /usr Specific directories name: the directory called name. : (one dote) the current directory.. : (two dotes) the parent directory of the current Examples: cd share/ cd../usr/include/ cd../../usr/include/ cd./ Working with files ls: view directory content cp: copy from a source to a destination rm: remove a file or a directory mv: rename and move objects (file and directories) Rename files: mv names.txt fullnames.txt Move files: mv [filename] [dest-dir] Working with files touch: create an empty file cat: view the content of an ascii file more: view the content of an ascii file more is a command to view the contents of a text file one screen at a time. tail, head: view the tail or head of an ascii file Working with files Display objects' names only : ls Display all objects' names only even hidden objects: ls -a Display a long list for each objects (even hidden file): ls –al Display objects' names with inode numbers : ls –i The inode is a data structure in a Unix-style file system that describes a filesystem object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data. Display the current directory content recursively :ls -R Working with files: ls Working with files: cp Syntax: cp [options ] source destination Often -i option is used to ask before overwriting Use -f option to force overwriting Examples: Copy the file file1 to directory dir1: cp file1 dir1 Create a copy of the file file1 with a name file2: cp file1 file2 Copy directory dir1 recursively to dir2: cp –r dir1 dir2 Create a copy of the directory dir1 with a name dir2: cp –r dir1 dir2 Working with files: rm Syntax: rm -[options ] f1 f2 f3 … fn Remove one or more files Often -i option is used to ask before removing Use -f option to force removing Use -r option to remove a non-empty directory recursively Examples: Remove the file called file1: rm –i file1 Remove the directory called dir1: rm –rf dir1 Remove two files called file1 and file2: rm –f file1 file2 Working with files: mv Syntax: mv -[options ] source destination Rename a file Move a file to another directory Move a directory to another directory Often -i option is used to ask before overwriting Use -f option to force overwriting Examples: Rename file1 to file2 : mv file1 file2 Move file1 to dir1 : mv file1 dir1 Move the directory dir1 to dir2: mv dir1 dir2 Working with files: touch Syntax: touch filename Create an empty file if the filename doesn’t exist. Modify the date of the last access of the file already exists and its content would not be lost. Working with files: cat Syntax: cat -[options ] f1 f2 f3 … fn Display the whole content of one or more files. To break: CTL+c To break with possibility to resume: CTL+s To resume: CTL+q Working with files: more Syntax: more -[options ] f1 f2 f3 … fn Working with files: tail and head Syntax: tail -[options] f1 f2 f3 … fn Display by default the 10 last lines. Use –n option to display n lines. head command is similar to tail it displays the first lines instead of last lines Working with files: counting Syntax: wc -[options ] filename This command is for line, word and character counts. -l : display the line count. -w : display the word count. -c : display the byte count. -m : display the character count File system structure There are many existing files and directories in the file system. Hierarchical organization: One root denoted : / First level: Many directories under the root ( / ): etc, bin, tmp, home,... Second level: contains many other FILES AND DIRECTORIES e.g. : /bin/ls, /home/user Third level: … File system structure Paths to objects The path to a specific object is a set of directories which terminates with that object name. Directories are separated with “/”. Absolute path: begins with / /usr/share/doc/gnuplot, /home/user Relative path: begins with a directory name../../var/log/messages Using symbolic links Files (and directories) might be located on several different file systems. You can use symbolic links to link files that are in different file systems but without spending more disk space. Use ln command to create a symbolic link If the original file is deleted, the soft link is broken. Example: ln -s source_file itslink Hard and Soft Links Briefly, A hard link always points a filename to data on a storage device. A soft link always points a filename to another filename, which then points to information on a storage device. This figure illustrates the concept: Hard and Soft Links Every file on the Linux filesystem starts with a single hard link. The link is between the filename and the actual data stored on the filesystem. Creating an additional hard link to a file means you create a new filename pointing to the exact same data as the old filename. This means that the two filenames, though different, point to identical data. If the original file is deleted, the data still exists under the secondary hard link. Files that are hard-linked together share the same inode number. List of commands you need to know Logs in as admin: sudo su Add new user (1): adduser username Logs in as new user: su – username Install app or package (1): apt-get install nameofpackage Install app or package (2): pip install nameofpackage Update your installation repository: apt-get update Module summary This module introduced main commands to work with files and directories Now you should be able to do the following: Copy files and directories Move and rename files and directories Create files and directories Remove files and directories Use symbolic links Learning Resources Linux Bible Chapter 4: Moving Around the Filesystem The Linux Command Line part 2,3 and 4. https://www.guru99.com/terminal-file-manager.html Hard and Soft (Symbolic Links): https://www.redhat.com/sysadmin/linking-linux-explained https://www.geeksforgeeks.org/soft-hard-links-unixlinux/amp/