User Tools

Site Tools


basic_file_and_directory_tasks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
basic_file_and_directory_tasks [2020/12/10 08:36] – [Basic File and Shell Operations] peteyboybasic_file_and_directory_tasks [2020/12/10 09:09] (current) – remove all the code tags i didn't need, use indent instead! peteyboy
Line 37: Line 37:
  
 Use single or double quotes around the file, for example: Use single or double quotes around the file, for example:
-<code>rm "my file.txt"</code>+  rm "my file.txt"
 or or
-<code>cp 'my file.txt' my_file.txt</code>+  cp 'my file.txt' my_file.txt
  
 Use a backslash where there's an empty space: Use a backslash where there's an empty space:
-<code>"mv my\ file.txt my_file.txt"</code>+  mv my\ file.txt my_file.txt
 or or
-<code>cd backups/text\ pages/</code>+  cd backups/text\ pages/
  
 ===Tab-Completion=== ===Tab-Completion===
Line 51: Line 51:
 Also, when naming a file using two or more words, the safest choices you have to use are the underscore, the dash, and the period. Examples are: Also, when naming a file using two or more words, the safest choices you have to use are the underscore, the dash, and the period. Examples are:
  
-<code> + 
-my_file.txt +  my_file.txt 
-my-file.txt +  my-file.txt 
-my.file.txt +  my.file.txt
-</code>+
  
 Or, you can keep it as one word: Or, you can keep it as one word:
  
-<code>myfile.txt</code>+  myfile.txt
  
 Using other symbols, like the ampersand, may cause problems because some of these symbols may mean other things to the shell. As always, for more information you should read the man-pages for the shell of your choice. Using other symbols, like the ampersand, may cause problems because some of these symbols may mean other things to the shell. As always, for more information you should read the man-pages for the shell of your choice.
Line 65: Line 64:
 ==== Command Tutorials ==== ==== Command Tutorials ====
  
-Note: in the examples below, the percent sign is used to denote the command prompt and is not meant to be typed.+:!: in the examples below, you'll be typing these commands at the shell prompt, so they will appear after a ''$'' or a ''%'' prompt. 
  
 === touch and pwd : create a file and print the working directory === === touch and pwd : create a file and print the working directory ===
Line 71: Line 71:
 To create a file without invoking a text editor or another program, one simply has to //touch// it. For example, to create a file called //orange.txt//, at the command prompt type: To create a file without invoking a text editor or another program, one simply has to //touch// it. For example, to create a file called //orange.txt//, at the command prompt type:
  
-<code>touch orange.txt</code>+  touch orange.txt
  
 Nothing much to that! To see the file you created you have the ability to list the file(s) and directories in the current working directory. First, let's see which directory we are in. By default, upon creating a ssh link or a telnet link to your shell account, you will be in your home directory. To confirm this, at the command prompt you can type: Nothing much to that! To see the file you created you have the ability to list the file(s) and directories in the current working directory. First, let's see which directory we are in. By default, upon creating a ssh link or a telnet link to your shell account, you will be in your home directory. To confirm this, at the command prompt you can type:
  
-<code>pwd</code>+  pwd
  
 If your user name is georgette, you may get something like this: If your user name is georgette, you may get something like this:
  
-<code>/udd/g/georgette</code>+  /udd/g/georgette
  
 Or if you're on your home computer, perhaps you'll see something like this: Or if you're on your home computer, perhaps you'll see something like this:
  
-<code>/home/georgette</code>+  /home/georgette
  
 === ls : list files in current directory === === ls : list files in current directory ===
Line 89: Line 89:
 Now to list the files in your current directory, type: Now to list the files in your current directory, type:
  
-<code>ls</code>+  ls
  
 If you followed the tutorial and created the //orange.txt// file with the //touch// command, you should see this file in what the //ls// command yields. Next, try //ls// with the various options below and see the difference in the kinds of information each option provides: If you followed the tutorial and created the //orange.txt// file with the //touch// command, you should see this file in what the //ls// command yields. Next, try //ls// with the various options below and see the difference in the kinds of information each option provides:
  
-<code> +  ls -l 
-ls -l +  ls -hl 
-ls -hl +  ls -a 
-ls -a +  ls -al 
-ls -al +  ls -ahl
-ls -ahl +
-</code>+
  
 === cp : copy a file === === cp : copy a file ===
Line 107: Line 105:
 Say you want to backup your //orange.txt// file to a sub-directory (more about creating directories in a moment) called //backups//. To do so, you would type the following: Say you want to backup your //orange.txt// file to a sub-directory (more about creating directories in a moment) called //backups//. To do so, you would type the following:
  
-<code>cp orange.txt backups/</code>+  cp orange.txt backups/
  
 The forward slash at the end of the word //backups// means that this is a directory. The forward slash at the end of the word //backups// means that this is a directory.
Line 113: Line 111:
 To use the //cp// command to change the name of the file without destroying the original you would type the following: To use the //cp// command to change the name of the file without destroying the original you would type the following:
  
-<code>cp orange.txt papaya.txt</code>+  cp orange.txt papaya.txt
  
 where //papaya.txt// is the new name of the file. where //papaya.txt// is the new name of the file.
Line 119: Line 117:
 And to copy the original //orange.txt// file to the backup directory and to change the name at the same time, you would type: And to copy the original //orange.txt// file to the backup directory and to change the name at the same time, you would type:
  
-<code>cp orange.txt backups/papaya.txt</code>+  cp orange.txt backups/papaya.txt
  
 === mv : move or rename a file === === mv : move or rename a file ===
Line 125: Line 123:
 The //mv// command works similarly to the //cp// command but with one vital difference. Moving a file means destroying the original file name. Thus the following command: The //mv// command works similarly to the //cp// command but with one vital difference. Moving a file means destroying the original file name. Thus the following command:
  
-<code>mv orange.txt papaya.txt</code>+  mv orange.txt papaya.txt
  
 essentially replaces the //orange.txt// with the new //papaya.txt// file. essentially replaces the //orange.txt// with the new //papaya.txt// file.
  
 You can keep the file name the same with the //mv// command by moving the file to a separate directory. To do so, type the following: You can keep the file name the same with the //mv// command by moving the file to a separate directory. To do so, type the following:
-<code>mv orange.txt backups/</code>+  mv orange.txt backups/
  
 This would move the //orange.txt// file to the backups directory. To move the file to the backups directory and to rename it then, you'd type: This would move the //orange.txt// file to the backups directory. To move the file to the backups directory and to rename it then, you'd type:
  
-<code>mv orange.txt backups/papaya.txt</code>+  mv orange.txt backups/papaya.txt
  
 === rm : remove a file === === rm : remove a file ===
Line 140: Line 138:
 Removing a file is also very simple. The command to do so is //rm//. To completely remove and destroy a file simply type: Removing a file is also very simple. The command to do so is //rm//. To completely remove and destroy a file simply type:
  
-<code>rm orange.txt</code>+  rm orange.txt
  
 === short note on interactive use === === short note on interactive use ===
Line 146: Line 144:
 The commands for copying, moving and removing files if carelessly used may wreak a bit of havoc for you. For these commands, you may want to invoke the interactive option by typing: The commands for copying, moving and removing files if carelessly used may wreak a bit of havoc for you. For these commands, you may want to invoke the interactive option by typing:
  
-<code> + 
-cp -i orange.txt backups/orange.txt +  cp -i orange.txt backups/orange.txt 
-mv -i orange.txt papaya.txt +  mv -i orange.txt papaya.txt 
-rm -i orange.txt +  rm -i orange.txt
-</code>+
  
 When the interactive option is called, you'll be prompted to answer yes or no to each file you are asking to be removed. For //cp -i// and //mv -i//, you'll be prompted if and only if the file you are copying to or moving will overwrite another file. When the interactive option is called, you'll be prompted to answer yes or no to each file you are asking to be removed. For //cp -i// and //mv -i//, you'll be prompted if and only if the file you are copying to or moving will overwrite another file.
Line 157: Line 154:
 The //file// command is useful to determine what type of file a file is. In unix-like operating systems, a file's name is fairly flexible and the file extension, e.g. the .txt appendage, is not always necessary. So if someone sent you a file and you wanted to be certain what type of file it was before you opened it, use the //file// command like so: The //file// command is useful to determine what type of file a file is. In unix-like operating systems, a file's name is fairly flexible and the file extension, e.g. the .txt appendage, is not always necessary. So if someone sent you a file and you wanted to be certain what type of file it was before you opened it, use the //file// command like so:
  
-<code>file name_of_file</code>+  file name_of_file
  
 The results for a text file would be something like this: The results for a text file would be something like this:
  
-<code>name_of_file: ASCII text</code>+  name_of_file: ASCII text
  
 Say someone sent you an image file called //something.something// in the PNG format and you wanted to be certain it was actually a PNG file, simply type: Say someone sent you an image file called //something.something// in the PNG format and you wanted to be certain it was actually a PNG file, simply type:
  
-<code>file something.something</code>+  file something.something
  
 If the file is truly a PNG file, you should see something similar to this: If the file is truly a PNG file, you should see something similar to this:
  
-<code>something.something: PNG image data, 922 x 691, 8-bit/color RGBA, non-interlaced</code>+  something.something: PNG image data, 922 x 691, 8-bit/color RGBA, non-interlaced
  
 ==== less : read a file ==== ==== less : read a file ====
Line 175: Line 172:
 The //less// command is a type of pager available to view and browse through text files without altering or opening the file in a text editor. You're encouraged to read the man-page for this command because it possesses many useful attributes such as searching through text for key words or strings. Invoke it with the name of the file you want to view: The //less// command is a type of pager available to view and browse through text files without altering or opening the file in a text editor. You're encouraged to read the man-page for this command because it possesses many useful attributes such as searching through text for key words or strings. Invoke it with the name of the file you want to view:
  
-<code>less orange.txt</code>+  less orange.txt
  
 If there is more text in the file than can fit on your computer screen, press the space-bar to scroll down page by page. Oftentimes, your Page Up and Page Down buttons on your keyboard will work and the arrow keys normally allow you to go up and down through the file line by line. If there is more text in the file than can fit on your computer screen, press the space-bar to scroll down page by page. Oftentimes, your Page Up and Page Down buttons on your keyboard will work and the arrow keys normally allow you to go up and down through the file line by line.
Line 183: Line 180:
 You create a directory by using the //mkdir// command. To create the backups directory we used in earlier examples, type: You create a directory by using the //mkdir// command. To create the backups directory we used in earlier examples, type:
  
-<code>mkdir backups</code>+  mkdir backups
  
 ==== cd : change directory ==== ==== cd : change directory ====
Line 189: Line 186:
 The //cd// command is used to change directories. If we are in our home directory and want to go to the newly created //backups// sub-directory, we'd simply type: The //cd// command is used to change directories. If we are in our home directory and want to go to the newly created //backups// sub-directory, we'd simply type:
  
-<code>cd backups</code>+  cd backups
  
 To go back simply type: To go back simply type:
- +  cd
-<code>cd</code>+
  
 Typing //cd// all by itself will always take you back to your home directory which is useful if you're deep in another branch of the directory tree. If you just want to go back up a level, type: Typing //cd// all by itself will always take you back to your home directory which is useful if you're deep in another branch of the directory tree. If you just want to go back up a level, type:
  
-<code>cd ..</code>+  cd ..
  
 And of course, you can always type the full path to the directory you want to change to: And of course, you can always type the full path to the directory you want to change to:
  
-<code>cd /usr/bin</code>+  cd /usr/bin 
  
 To switch back to the previous working directory, type: To switch back to the previous working directory, type:
  
-<code>cd -</code>+  cd -
  
 ==== rmdir : remove a directory ==== ==== rmdir : remove a directory ====
Line 211: Line 207:
 And to remove an empty directory, you use the the //rmdir// command. And to remove an empty directory, you use the the //rmdir// command.
  
-<code>rmdir backups</code>+  rmdir backups
  
 The //rmdir// will only work if the directory you want to remove is empty of files. If a directory contains files in it and you are sure you want to remove said directory along with all the files in it, you actually have to go back to the //rm// command and type: The //rmdir// will only work if the directory you want to remove is empty of files. If a directory contains files in it and you are sure you want to remove said directory along with all the files in it, you actually have to go back to the //rm// command and type:
  
-<code>rm -r name_of_directory</code>+  rm -r name_of_directory
  
 The //-r// is a command option telling the //rm// or remove command to delete the directory and all contents including subdirectories. It stands for //recursive//. Be very careful about using this command! In fact, a better way to run this command is by typing: The //-r// is a command option telling the //rm// or remove command to delete the directory and all contents including subdirectories. It stands for //recursive//. Be very careful about using this command! In fact, a better way to run this command is by typing:
  
-<code>rm -ir</code>+  rm -ir
  
 This invokes the interactive use of the remove command which prompts you to answer yes or no to each file and directory to be possibly removed. Again, read the man-pages for more details. This invokes the interactive use of the remove command which prompts you to answer yes or no to each file and directory to be possibly removed. Again, read the man-pages for more details.
Line 227: Line 223:
 Finally, to clear the screen type the following at the prompt: Finally, to clear the screen type the following at the prompt:
  
-<code>clear</code> +  clear 
- ----+ 
 +----
  
 legacy link: http://sdf.org/?tutorials/file_operations ,v 1.11 2009/11/28 10:39:54 rogerx Exp $ legacy link: http://sdf.org/?tutorials/file_operations ,v 1.11 2009/11/28 10:39:54 rogerx Exp $
  
  
basic_file_and_directory_tasks.1607589383.txt.gz · Last modified: 2020/12/10 08:36 by peteyboy