Essential Linux Commands for File Management

Classified in Computers

Written at on English with a size of 4.57 KB.

Essential Linux Commands for File Management

Basic Operations

  • mkdir /Destination/Name: Creates an empty directory named Name at the specified Destination.
  • touch /Source: Updates the access and modification date of the Source file to the current time. If the file does not exist, it creates an empty file.

Copying Files

  • cp /Source/Destination: Copies the Source file to the Destination with the same name.
  • cp -r /Source/Destination: Recursively copies files from the Source directory to the Destination directory, preserving the directory structure and file names.

Moving Files

  • mv -f /Source/Destination: Moves the Source file to the Destination, overwriting any existing file with the same name.
  • mv -u /Source/Destination: Moves the Source file to the Destination only if the Source file is newer than the Destination file or if the Destination file does not exist.
  • mv -i /Source/Destination: Moves the Source file to the Destination, prompting for confirmation before overwriting an existing file with the same name.

Deleting Files

  • rm -f /Source: Deletes the Source file without prompting for confirmation.
  • rm -Rf /Source: Recursively removes all files and subdirectories within the Source directory, and finally removes the Source directory itself.

Listing Files

  • ls /Source: Lists the contents of the Source directory.
  • ls -a /Source: Lists the contents of the Source directory, including hidden files (files starting with a dot).
  • head /Source: Prints the first 10 lines of the Source file to the screen.
  • head -n N /Source: Prints the first N lines of the Source file to the screen.
  • tail /Source: Prints the last 10 lines of the Source file to the screen.
  • tail -n N /Source: Prints the last N lines of the Source file to the screen.
  • cat /Source: Prints the entire content of the Source file to the screen.
  • tac /Source: Prints the content of the Source file to the screen in reverse order (last line first).
  • wc /Source: Prints the number of lines, words, and bytes in the Source file.
  • wc -w /Source: Prints the number of words in the Source file.
  • wc -L /Source: Prints the number of characters in the longest line of the Source file.

Renaming Files

  • rename "FROM" "TO" /Source: Replaces the first occurrence of "FROM" with "TO" in the names of files within the Source directory.
  • rename ".jpg.png" ".png" /Source: Renames files in the Source directory, changing the extension ".jpg.png" to ".png".

Concatenating Files

  • cat /Source1 /Source2: Concatenates Source2 and then Source1 and prints the result to the screen.
  • cat /Source1 /Source2 > /Destination: Concatenates Source2 and then Source1 and writes the result to the Destination file.

Splitting Files

  • split -d -aN -bM /Source Name: Splits the Source file into multiple files of size M (e.g., Mk for kilobytes, Mm for megabytes). The resulting files will be named Name followed by a numeric suffix of length N. This is useful for testing hard drive performance with small files.
  • split -d -a3 -b1400k /Source Name: Splits the Source file into multiple files of size 1400KB (suitable for floppy disks), named Name followed by a three-digit numeric suffix.

Entradas relacionadas: