Skip to content

Instantly share code, notes, and snippets.

@noynaert
Last active January 27, 2017 16:57
Show Gist options
  • Save noynaert/adf43d6e65670d62eef5e30afeb1067e to your computer and use it in GitHub Desktop.
Save noynaert/adf43d6e65670d62eef5e30afeb1067e to your computer and use it in GitHub Desktop.
Linux and Unix file names, including command line operations

#Linux and Unix files and directories at the command line

The same rules apply to file names and directory names, but conventions vary a bit. For example, file names often have extensions, but directory names seldom do.

File Names

File names that start with a period . are "hidden" files

A good set of guidelines is at https://www.cyberciti.biz/faq/linuxunix-rules-for-naming-file-and-directory-names/

Summary of the key points:

  • File names are case sensitive.
    • lowercase and camelCase are common
    • They can be long, but keep them short if possible
    • Avoid blanks if possible
    • If you encounter a name with a blank or a troublesome special character
      • Use a backslash followed by a space for blanks For example: my\ file
      • Enclose the entire file name in " or ' marks. Similar rules to php. " allows for variable expansion echo 'My path is $PATH' echo "My path is $PATH"

Extensions

Some file (and directory) names have extensions. This is a period followed by a few letters

Some common extensions in linux

  • .txt A text file
  • .tar A group of files combined into a single file. Also called an "archive"
  • .gz, *.bz A compressed file
  • .tar.gz An archive that has also been compressed (like a zip)
  • .zip A compressed archive that is mostly compatable with windows zip files
  • .sh A "shell script" or miniature program

commands

  • ls List the files
  • ls -l Give a long, or detailed listing of files
  • ls -a List all files, including hidden files
  • ls -al Give a long listing of all files
  • ls -d give a directory listing
  • touch Create a file
  • rm Remove a file
  • cd Change directory
  • mkdir Create a directory
  • rmdir Remove an empty directory
  • rm -r Recursively remove all files and subdirectories

Showing text files

  • cat filename -- Short for "contatinate." It lists the entire file. Long files will scroll off the screen

  • less filename -- Shows the file a line at a screen at a time. *Page Up and Page down keys work. Spacebar is like Page Down. Up and down arrows move one line at a time. q to exit.

  • nano filename Edits a file. ^ means "Ctrl"

  • head filename -- Shows the first 10 lines "head -n 5 filename " would print 5 lines

  • tail filename -- prints the end of the list

  • lp filename -- Sends the file to a printer, if a printer is set up.

Other miscellaneous commands

  • man commandname Help file
  • exit -- Exits from the browser.
  • cp sourse destination Copies from a file to a file. The second argument has to be supplied. The second argument can be a filename or a directory name. Often you want to copy to the current directory, so use a period as the second argument. You might do something like cp ../data.txt .
  • mv *source destination Moves a file. It works like cp. mv can also be used to rename files

Home and special folder symbols

/home/username is your "home" folder

~ (tilde) is a shorthand for "home . is the current directory .. is the parent directory. ../.. would be the "grandparent" directory

Command Line special characters

  • ^c "Break"
  • ^d Endo of file
  • ; Put another command on the same line, and always execute the second command
  • && Put another command on the same line, but only execute the second command if the first command is successfule
  • Execute the command in the background. You wwill most commonly use this if you launch a gui app from the command line. But be careful because closing the terminal will also abort the gui program without warning.
  • Redirect output to a file

  • < Redirect input from a file
  • | "Pipe" the output of one command as input to the next command. For example cat someFile | sort | less
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment