Main Menu

search

You are here

Linux: File and Directory Commands

[last updated: 2024-09-20]
Linux home page
Linux commands
directory listing colors
-----

  • Note: Since Linux treats everything as a file, all commands that work on files will also work on directories.
    However commands listed below as 'directory' commands, will Only work on directories.
    -----------------------------------

  • File Commands: (find, cp, mv, rm)

    • Note: If moving or copying files into protected directories (like eg. /etc), then you must be logged in as root, or else precede the command with sudo.

    • Find a File or Directory:      
      • $ find -name [fileName]
        searches all subdirectories of current directory for [fileName]
      • $ find -name [fileName] *
        searches for all files whose name starts with [fileName]
      • $ find / -name [filename] ... Searches the whole system for filename and outputs a list of all directories that contain the file.
      • Other parameters:
        -type f ... just searches for files
        -type d ... just searches for directories

      ----------

    • Copy:      
      • (link to:) cyberciti - cp copy command
      • $ cp [options] [source] [destination]
        • options:
          -a : preserve attributes
          -v : verbose output
          -r : copy directories recursively (ie. including sub-directories)
      • Copy all the files and subdirs in a directory to another (existing) directory:
        $ cp -v [sourceDir]/* [destDir]
      • see link above for instructions on using rsync for copying, specifically for backing up network files...

      ----------

    • Move:      
      $ mv [source]   [destination]
      tested as follows: used cd navigation to move into directory where file was located (source)
      typed mv, then complete filename as 'source'
         then path to destination directory. It worked
      used sudo mv since the destination directory was in root structure.
    • $ mv [oldname]   [newname] ... to rename a file or directory
    • ----------

    • Delete:      
      $ rm [fileName]

    ===========================================

  • Directory Commands: (pwd, cd, ls, mkdir)
    • $ pwd ... print name of working directory
      • $ pwd /
        prints name of root directory, which is the top-most directory in your main memory (the SD card on a rPi).

    • $ cd [options] ... change directory.
      • $ cd Desktop ... changes your current directory to the sub-folder 'Desktop' that it assumes is located in your current folder
        If there is no such folder, it will give you an error with that message.
      • $ cd .. (including the "..") moves you up one level in your file structure. So if you had been in your home directory '/home/pi', you will now be in '/home'.
      • $ cd / ... moves you to your root directory
      • $ cd /[path] ... moves you to wherever you want, whatever path referenced from your root directory ('/').

    • $ ls ... list directory contents
      • $ ls -l ... (lower case 'L') shows permissions on all files in directory
      • $ ls -a ... shows all files including hidden files
      • $ ls -1a ... shows all files, one listing per line

    • $ mkdir [new dirName] ... Create a new directory
      depending on where you are, you may need to execute this as root with sudo
    • $ rm -r [dirName] ... delete a directory that is not empty:
      You will be prompted for each non-empty or write-protected directory or file,
      so if you want to avoid that, instead do:
      $ rm -rf [dirName]
      • Sometimes this will fail for error: directory still busy.
        Have not found a solution to this yet...

    • Make a shortcut on desktop to a folder/directory
      • select the source folder in the File Manager
      • from the Edit menu select Create/Make Link
      • a link will be created in your current folder
      • move the link to Desktop
      • clicking on the link in the desktop will open the File Manager and display chosen directory

    ===========================================

  • Ownership and Permission commands::
    • chown

    -------------------------------------------------------

  • "Unzip" a .tar.gz file:
      tar -xvzf [filename].tar.gz

    .

    .

    .

    eof