List of commands that will be introduced here:
ls #view contents of a directory
touch #create new file
mkdir #create new directory
cp #copy contents of one file to another
mv #move file from one location to another
rm #delete a file or directory
ls examples
jennifer@my_computer:~/Desktop$ ls
c++ projects Research Paper.docx test.cpp
shows the current directory
jennifer@my_computer:~/Desktop$ ls c++\ projects
DNA_analysis.cpp encryption.cpp pool_game.cpp
shows the directory "c++ projects". Space characters in file names are typed as "\ ".
touch example
jennifer@my_computer:~/Desktop$ ls
c++ projects Research Paper.docx test.cpp
jennifer@my_computer:~/Desktop$ touch ruby_test.rb
jennifer@my_computer:~/Desktop$ ls
c++ projects Research Paper.docx ruby_test.rb test.cpp
mkdir example
jennifer@my_computer:~/Desktop$ mkdir ruby
jennifer@my_computer:~/Desktop$ ls
c++ projects Research Paper.docx ruby ruby_test.rb test.cpp
jennifer@my_computer:~/Desktop$ cd ruby
jennifer@my_computer:~/Desktop/ruby$ ls
<nothing>
jennifer@my_computer:~/Desktop/ruby
It doesn't actually print <nothing>
. It's just how I'm representing that it doesn't output anything
cp examples
jennifer@my_computer:~/Desktop/ruby$ cd ..
jennifer@my_computer:~/Desktop$ cp test.cpp c++_test.cpp
jennifer@my_computer:~/Desktop$ ls
c++ projects c++_test.cpp Research Paper.docx ruby ruby_test.rb
test.cpp
This is when the last arg to cp
, in this case "c++_test.cpp" is not an existing directory. cp
will create a file called "c++_test.cpp", with contents identical to that of "test.cpp". If c++_test.cpp already existed, cp
would have deleted what was previously there before copying the contents of "test.cpp" over.
jennifer@my_comptuer:~/Desktop$ ls ruby
<nothing>
jennifer@my_computer:~/Desktop$ cp ruby_test.rb ruby
jennifer@my_computer:~/Desktop$ ls ruby
ruby_test.rb
This is what happens when the last arg to cp
, in this case "ruby", is a directory. cp
creates a file with the same name as "ruby_test.rb", but in the directory "ruby".
mv examples
jennifer@my_computer:~/Desktop$ ls
c++ projects c++_test.cpp Research Paper.docx ruby ruby_test.rb
test.cpp
jennifer@my_computer:~/Desktop$ mv ruby_test.rb ruby\ test.rb
jennifer@my_computer:~/Desktop$ ls
c++ projects c++_test.cpp Research Paper.docx ruby ruby test.rb
test.cpp
This is what happens when the last arg to mv
, in this case "ruby test.rb", is not an existing directory. The file "ruby_test.rb" has been renamed to "ruby test.rb". If "ruby test.rb" already existed, it would have been overwritten Note, again, that spaces are preceded by a ''.
jennifer@my_computer:~/Desktop$ ls
c++ projects c++_test.cpp Research Paper.docx ruby ruby test.rb
test.cpp
jennifer@my_computer:~/Desktop$ ls c++\ projects
DNA_analysis.cpp encryption.cpp pool_game.cpp
jennifer@my_computer:~/Desktop$ mv test.cpp c++\ projects
jennifer@my_computer:~/Desktop$ ls
c++ projects c++_test.cpp Research Paper.docx ruby ruby test.rb
jennifer@my_computer:~/Desktop$ ls c++\ projects
DNA_analysis.cpp encryption.cpp pool_game.cpp test.cpp
This is what happens when mv
is a directory that already existed. The file "test.cpp" gets moved to the directory "c++ projects".
rm examples
jennifer@my_computer:~/Desktop$ ls
c++ projects c++_test.cpp Research Paper.docx ruby ruby test.rb
jennifer@my_computer:~/Desktop$ rm c++_test.cpp
jennifer@my_computer:~/Desktop$ ls
c++ projects Research Paper.docx ruby ruby test.rb
c++_test.cpp has been deleted
jennifer@my_computer:~/Desktop$ rm c++\ projects
rm: cannot remove 'c++ projects': Is a directory
jennifer@my_computer:~/Desktop$ ls
c++ projects Research Paper.docx ruby ruby test.rb
rm
has an extra requirement to delete directories
jennifer@my_computer:~/Desktop$ rm -rf c++\ projects
jennifer@my_computer:~/Desktop$ ls
Research Paper.docx ruby ruby test.rb
-rf
must be added to delete a directory.
To learn more about ls
, type the command ls --help
. For touch
, type touch --help
. Likewise with all 6 commands mentioned here. This prints out a detailed explanation of use without creating or deleting anything.