Skip to content

Basic Files

Navigating files is basic concepts. To do anything in linux you need to be able to navigate the file system, edit, list, move, copy, delete, and create files. This section will go over commands needed to achieve these tasks. As a bonus we will look at help and man in this section. As it will help you use commands.

Commands

help and man

Most commands have a help switch this is usually --help or -h. To get help for a command you would type the command and --help. For instance to get help for the ls command you would type ls --help. This will display the help information for this command. This will tell you the different switches you can use with the command to alter how the command functions.

help example

ls --help

Another way to get help is to use man. Man is short for manual. This will give you the manual for a command. To use man you would type man and then the command name. For instance to get man for ls you would type man ls.

man example

man ls

One other option is to search man if you are not sure of a command. Say you want to find commands that will compress or zip up a file or directory you can use man -k zip. This will give a list of commands that will zip or compress a file or directory

man search example

man -k zip

cd

This command is used to change directories. You can use it as relative to the current path or absolute. You can go multiple levels at a time. To change to a subdirectory of the current path you would use cd subdir or to go to a subdirectory of a subdirectory you can do cd subdir1/subdir2. To do an absolute path put the full complete path in cd /var/log. To go to parent directories you would use two dots .. to go up multiple levels you can two dots separated by a slash per level cd ../../.. would take you up the directory levels. cd also has a few specials you can use cd without any arguments will take you back to your home directory. cd ~ also takes you back to your home directory(~ is a shortcut to your home directory, so if you wanted to switch to downloads in your home directory you could do cd ~/Downloads). You can also switch to your previous directory. Lets say you are in ~/Downloads, and the you use cd ../Documents. You are now in ~/Documents, but if you want to go back to Downloads, you can cd -.

cd examples

cd subdir
cd subdir/subdir
cd /var/log
cd ../../..
cd
cd ~
cd ~/Downloads
cd -

ls

ls list files. Without any arguments it will list the files of the current directory. If you specify a path after ls it will list files of that path. I encourage to read through help at the different switches available but it common to give the -l switch or the -a switch. -l lists with extended details like permissions and owner. -a does all files including hidden.

ls example

ls
ls -l
ls -a
ls -al

mv

This moves a file from location to another. This can be from any directory to another directory provided you have the correct privileges in both. This command is mv originalfileordir newfileordir. Another options is to move a file from current location to a subdir mv file1 subdir/

mv examples

mv file1 file2
mv dir1 dir2
mv file1 subdir/

cp

Often files are needed to be copied. This can be to create a backup of a file, or maybe an alternative configuration. cp can also be used to create a copy of a directory, but you need to add the -r/-R switch to make it recursive

cp examples

cp file1 file1.bak
cp -r dir1 newdir
cp -R dir1 newdir

rm

rm is used to remove files or directories. Again I recommend reading the help or man page for this command. You can remove multiple files with listing each file to remove rm file1 file2. rm will not work on a directory without the recursive switch -r. I also recommend using -f when using the -r otherwise you will get a confirmation prompt for each file or directory. rm -rf dir1 dir2

rm examples

rm file1
rm file1 file2
rm -rf dir1
rm -rf dir2

mkdir

Creating a directory is done with mkdir. Without any switches mkdir will only create one directory at a time. To create a directory called subdir you would use mkdir subdir. To create subdir/subdir1 subdir has to exist first so you would have do mkdir subdir then mkdir subdir/subdir1. There is a shortcut though with the -p switch it will create all directories in a path so you do mkdir -p subdir/subdir1

mkdir example

mkdir subdir
mkdir subdir/subdir1
# or do
mkdir -p subdir/subdir1

pwd

Use the pwd command to print the directory you are currently in

pwd example

pwd

create and edit files

There are multiple ways to create or edit files. To create an empty file you can use touch filename. If you need to put content in the file you would use an editor like vi, vim, nvim, or nano. Many distributions install vi or vim by default. If using vi, vim, or nvim there are a few basic concents you need to understand. These are modal editors. When you first open you are in normal mode. In normal you can navigate around or enter other modes like command or insert. To enter insert or edit mode, while in normal mode, simply hit the i key. This will allow you to type, paste, or delete text. To exit insert mode simply hit the esc key. To save a file you have to get into command mode. From normal node simply entry :. This will put a prompt at the bottom prefixed with :. You would use w to write the file(save). You would use q to quit out of the editor. If you try and quit and there are changes that have been made you will get an error. In this case if you want to quit without saving do q!. If you want to save and quit at one time you can do wq. This is not all inclusive of vim, but it gives you a basic starting point (If you want a better understanding vi,vim, or nvim, launch the program and enter :Tutor). nano is a little simpler to use but is often not installed on servers. with nano there are hints at the bottom with how to do something. ^ means your ctrl key. So ^X means ctrl + X this will exit nano.

touch,vim,nano examples

touch file
vim file
nano file

Exercise

Note

On AWX https://tower.tuckhome.lan use the Basic Files - Setup, Basic Files - Grade, Basic Files - Cleanup

Tasks

All tasks are done in your home directory on the lab1.tuckhome.lan server.

  • Copy the copyme file to copyme.bak
  • Delete the file removeme
  • Create the directories newdir/subdir
  • Create an empty file with the name emptyfile
  • Move the moveme file to movedfile
  • Delete the directory removedir
  • Copy the copyme file to newdir/subdir
  • Create a file with the contents "This is a newfile". Name the file newfile
Solution
  • Copy the copyme file to copyme.bak
    cp copyme copyme.bak
    
  • Delete the file removeme
    rm removeme
    
  • Create the directories newdir/subdir
    • Option 1 create the directories individually
      mkdir newdir
      mkdir newdir/subdir
      
    • Option 2 create them at one time
      mkdir -p newdir/subdir
      
  • Create an empty file with the name emptyfile
    • Option 1 using touch
      touch emptyfile
      
    • Option 2 open an editor like vim
      vim emptyfile
      #once in vim enter command :wq to write and quite
      
  • Move the moveme file to movedfile
    mv moveme movedfile
    
  • Delete the directory removedir
    rm -rf removedir
    
  • Copy the copyme file to newdir/subdir
    cp copyme newdir/subdir/
    
  • Create a file with the contents "This is a newfile". Name the file newfile
  • Option 1 use an editor
    vim newfile
    # enter i to go into insert mode
    # type This is a newfile
    # hit excape to go back to normal mode
    # enter command :wq
    
  • Option 2 echo and redirect this want covered but a bonus
    echo "This is a newfile" > newfile