"ls" command



4910455-3175009525078105mobile itnTSolutions, LLC.00mobile itnTSolutions, LLC.4572002857500Linux Systems AdministratorProgramClass Notes:“Working with Files”00Linux Systems AdministratorProgramClass Notes:“Working with Files”sWorking with Files: - An important command-line task is managing the files in the directories. Most commonly used commands…allows us to see directory contents (files) and subdirectories contained in the pwd(current working directory)“touch” command:easy way to create an empty fileFor example: # touch file42OPTIONS:Another example using –t: # touch -t 200505050000 BigBattle.txtCreates a file using a specified time-a: change the access time only-c: if the file doesn’t exist, do not create it-d: update the access and modification times-m: change the modification time only-r: use the access and modification times of file-t: creates a file using a specified time“ls” command:Used to List FilesTo manage files on your server, you must first know what files are available. For this purpose the “ls” command is used.ls options:-l: shows file or directory, size, modified date and time, file or folder name and owner of file and it’s permission.. also known as long listing format-a: list all files (including hidden files)-d: list directories as plain files-F: Display a slash (`/') immediately after each pathname that is a directory,an asterisk (`*')after each that is executable, an at sign (`@')after each symbolic link, an equals sign (`=') after each socket, a percent sign (`%') after each whiteout, and a vertical bar (`|')after eachthat isa FIFO.More “ls” options:-h: In long format listings, display file sizes in human-readable format rather than in Bytes (Example: # ls –lh)-r: Display results in reverse order. Normally, ls displays its results in ascending alphabetical order-S: Sort results by file size-t: Sort by modification time"ls" Long listing Fields:Command: ls –l : -rw-r—r-- 1 root root 3576296 2014-04-03 11:05 logo-Ebuntu.pngExample: ls -l /etcColumn Breakdown Below:Field 1Field 2Field 3Field 4Field 5Field 6Field 7Field 8Field 9Field 10-rw-r--r--1rootroot209Mar 30 17:41printcapThe first field could be- for File, d for Directory, l for LinkThe second,third,fourth fieldsThose are permissions that means read, write and execute, and comes in three different fields that belongs to the permission the: (see permissions below)second: The owner has over the filethird: The group has over the filefourth: Everybody else has over the fileThe fifth fieldThis field specifies the number of links or directories inside this directory.The sixth field is the userThe user that owns the file, or directoryThe seventh field is the groupThe group that file belongs to, and any user in that group will have the permissions given in the third field over that file.The eighth fieldThe size in bytes, you may modify this by using the -h option together with -l this will have the output in k,M,G for a better understanding.The ninth fieldThe date of last modificationThe tenth fieldThe name of the fileFile permissions are explained below;three sets of characters, three times, indicating permissions for owner, group and other: r = readablew = writablex = executableFor Example: -rwxrw-r--, this means the line displayed is:a regular file (displayed as -)readable, writable and executable by owner (rwx)readable, writable, but not executable by group (rw-)readable but not writable or executable by other (r--)Examples Below: "ls" command1. List Files using ls with no optionls with no option list files and directories in bare format where we won’t be able to view details like file types, size, modified date and time, permission and links etc.2 List Files With option –lHere, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and it’s permission.-- called viewing directory in “long” format3. View Hidden FilesList all files including hidden file starting with ‘.‘. ls -a4. List Files with Human Readable Format with option -lhWith combination of -lh option, shows sizes in human readable format.ls –lh5. List Files and Directories with ‘/’ Character at the endUsing -F option with ls command, will add the ‘/’ Character at the end each directory.ls -F6. List Files in Reverse OrderThe following command with ls -r option display files and directories in reverse order.ls -r7. Recursively list Sub-Directories (Recuresively means doing something over again)ls -R option will list very long listing directory trees. See an example of output of the command.ls -R8. Reverse Output Order (****)With combination of -ltr will shows latest modification file or directory date as last.ls -ltr9. Sort Files by File SizeWith combination of -lS displays file size in order, will display big in size first.ls lS12. Show Help PageList help page of ls command with their option.ls –help"rm" Command--- “ rm “ command--> Cleaning up the file system is a task that needs to be performed on a regular basis. This command is used for this purpose--> Removes files or directories-- “rm” options:-f, --force / Ignore nonexistent files and never prompt before removing-i / prompt before every removal-I / prompt once before removing more than 3 files, or when removing recursively-r , -R, --recursive / Remove directories and their content recursivelyExamples:To remove a file whose name begins with a dash ("-"), you can specify a double dash ("--") separately before the filename. This is necessary so that rm does not misinterpret the filename as an option.Example: "-file.txt", you can delete it with the commandrm -- -file.txtOr, you can delete it by referring to it with a pathname. Example:, if the file "-file.txt" was located in the directory "/home/chope", you could delete it using:rm /home/chope/-file.txtor, if /home/chope is your current directory,rm ./-file.txtMore “rm” Examplesrm myfile.txtRemove the file myfile.txt. If the file is write-protected, you will be prompted to confirm that you really want to delete it.rm -f myfile.txtRemove the file myfile.txt. You will not be prompted, even if the file is write-protected; if rm can delete the file, it will.rm *Remove all files in the working directory. If it is write-protected, you will be prompted before rm removes it.rm -f *Remove all files in the working directory. rm will not prompt you for any reason before deleting them.rm -i *Attempt to remove every file in the working directory, but prompt before each file to confirm.rm -I *Remove every file in the working directory; prompt for confirmation if more than three files are being deleted.rm -r mydirectoryRemove the directory mydirectory, and any files and directories it contains. If a file or directory that rm tries to delete is write-protected, you will be prompted to make sure that you really want to delete it.rm -rf mydirectorySame as the above command, but you will never be prompted; if rm can delete the files, it will.---> Important Note: Be very careful when using "rm" this way. . .it can result in very serious consequences.. For example, if I typed "rm -rf / somedir (with a space between / and somedir), instead of rm -rf /somedir... everything would be removed in root 1st, then the somedir would attempted to be removed as the 2nd step...- “ cp “ (copy) command: ---> Copies files and directories ---> For example: cp is the command which makes a copy of your files or directories. For instance, let's say you have a file named picture.jpg in your working directory, and you want to make a copy of it called picture-02.jpg. You would run the command:cp picture.jpg picture-02.jpg...and the file will be copied. Here, picture.jpg is the source of the copy operation, and picture-02.jpg is the destination. Both files now exist in your working directory. Another Example:The source and destination files may also reside in different directories. For instance,cp /home/chuck/pictures/picture.jpg /home/chuck/backup/picture.jpg...will make a copy of the file /home/chuck/pictures/picture.jpg in the directory /home/chuck/backup. The destination file will also be named picture.jpg.(If you are the user chuck, you can abbreviate your home directory ("/home/chuck") using a tilde ("~"). For instance,cp ~/pictures/picture.jpg ~/backup/picture.jpgfunctions the same as the above command when it is run by chuck.) cp command main options:optionscp -aarchive filescp -fforce copy by removing the destination file if neededcp -iinteractive – to prevent overwriting [cp –i SindoDeMayo file42]cp -llink files instead of copycp -Lfollow symbolic linkscp -nno file overwritecp -Rrecursive copy (including hidden files) [copy entire directory : # cp –r dir42/ dir33]cp -uupdate - copy when source is newer than destcp -vverbose - print informative messagesExamples:Copying Multiple Files To A DirectoryYou may want to copy multiple files into another directory. To accomplish this, you can specify multiple files as the source, and a directory name as the destination. Let's say you are the user student, and you have a bunch of files in the directory /home/sally/pictures/ named picture-01.jpg, picture-02.jpg... and you want to copy them into the directory /home/sally/picture-backup/. This command will do the trick:cp ~/pictures/picture-*.jpg ~/picture-backupHere, we use a wildcard (the asterisk, "*") to indicate that the source files are all the files in the directory /home/sally/pictures whose name starts with "picture-" and has the extension ".jpg". They will be copied into the directory /home/sally/picture-backup, assuming that directory already exists. (If it doesn't exist, cp will give you an error message, and no files will be copied.)You can also specify multiple source files one after the other, and cp will expect that the final argument is a directory name, and copy them all there. For instance,cp ~/pictures/picture-01.jpg ~/pictures/picture-02.jpg ~/picture-backup...will copy only those two files, /home/sally/picture-01.jpg and /home/sally/picture-02.jpg, into the directory /home/sally/picture-backup.Copying Files RecursivelyYou can use cp to copy entire directory structures from one place to another using the -r option to perform a recursive copy. Let's say you are the user steve and you have a directory, /home/steve/files, which contains many files and subdirectories. You want to copy all those files, and all the subdirectories (and the files and subdirectories they contain), to a new location, /home/steve/files-backup. You can copy all of them using the command:Example: cp -r ~/files ~/files-backup...and the entire directory structure will be copied to the directory /home/steve/files-backup. When performing a recursive copy:if the directory files-backup already exists, the directory files will be placed inside;if files-backup does not already exist, it will be created and the contents of the files directory will be placed inside it.“mv” command:moves or renames files and directories on your filesystemExamplesmv myfile.txt myfilesMove the file myfile.txt into the directory myfiles. If myfiles is a file, it will be overwritten. If the file is marked as read-only, but you own the file, you will be prompted before overwriting it.mv myfiles myfiles2If myfiles is a file or directory, and myfiles2 is a directory, move myfiles into myfiles2. If myfiles2 does not exist, the file or directory myfiles is renamed myfiles2.mv myfile.txt ../Move the file myfile.txt into the parent directory of the current directory.mv -t myfiles myfile1 myfile2Move the files myfile1 and myfile2 into the directory myfiles.mv myfile1 myfile2 myfilesSame as the previous command.mv -n file file2If file2 exists and is a directory, file is moved into it. If file2 does not exist, file is renamed file2. If file2 exists and is a file, nothing happens.mv -f file file2If file2 exists and is a file, it will be overwritten.mv -i file file2If file2 exists and is a file, a prompt is given:mv: overwrite 'file2'?Entering "y", "yes", "Yes", or "Y" will result in the file being overwritten. Any other input will skip the file.mv -fi file file2Same as mv -i. Prompt before overwriting. The f option is ignored.mv -if file file2Same as mv -f. Overwrite with no prompt. the i option is ignored.mv My\ file.txt My\ file\ 2.txtRename the file "My file.txt" to "My file 2.txt". Here, the spaces in the file name are escaped, protecting them from being interpreted as part of the command.mv "My file.txt" "My file 2.txt"Same as the previous command.mv "My file.txt" myfilesAdditional Examples:To move a file to another directory and give it a new name, type the following: mv intro manual/chap1To move a file to another directory, keeping the same name, type the following: mv chap3 manualTo rename a file, type the following: mv appendix apndx.aEXERCISE: Working with Files ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download