7 - Working with Files in Linux

List of commands that allow us to manipulate files

  • 1- cp
  • 2- mv
  • CP command -this command will be used to copy files and directories. Syntax
        cp [OPTION] "source file name" "destination file name"
      
        cp [OPTION] "source dir" "destination dir name"
       
    Option

    For detailes about option for cp type this line at the bash line cp man or cp info Syntax

        cp "file name" "new location"
       
    or by using the full path
        cp /home/wannabedba/documents/"file name" /home/"new loaction"
      
    Examples
        cp file1 file2
      
    -Copies the contents of file1 into file2. If file2 does not exist, it is created; - otherwise, file2 is overwritten with the contents of file1.
      cp file1 dir1
      
    -Copy the contents of file1 (into a file named file1) inside of directory dir1.
      cp -R dir1 dir2
      
    -Copy the contents of the directory dir1. If directory dir2 does not exist, it is created. - otherwise, it creates a directory named dir1 within directory dir2. MV command Renames a file or moves it from one directory to another directory. Syntax
      mv [option] oldname newname
      
    Option

    [-f] mv will move the file(s) without prompting even if it is writing over an existing target. Note that this is the default if the standard input is not a terminal.

    [-i] Prompts before overwriting another file.

    [oldname] The oldname of the file renaming.

    [newname] The newname of the file renaming.

    [filename] The name of the file you want to move directory - The directory of were you want the file to go. Example

      mv myfile newdir/
      
    Moves the file myfile to the directory newdir.
      mv file.txt wannabedba.txt
      
    Moves (renames) the file " file.txt" to wannabedba.txt.
      mv file1.txt file2.txt file3.txt directory1
      
    The files file1.txt, file2.txt, file3.txt are moved to directory directory1. Directory1 must exist or mv will exit with an error.