![](https://aindien.com/media/posts/166/trianglify-lowres1.png)
Working With Files In Linux
These are my notes on working with files in linux.
The following commands are what makes working with the command line worth it.
All of these tasks can be performed in a graphical environment, but when you get
used to the command line, they become much faster.
Wildcards
Wildcards is one of the things that makes the command line so strong. They give
us a lot of flexibility. Wildcards allow you to select filenames based on
patterns of characters.
* matches any characters
? matches any single character
Using wildcards makes it possible to create complicated search queries.
* all files
a* any file begining with a
a*.txt any file beginning with a followed by characters and ending with .txt
file??? any file beginning with the name file and followed by exactly 3
characters.
Wildcards can be used with any command that accepts filenames as arguments.
Creating Directories
The mkdir command is used to create directories.
mkdir directory-name
We can also make several directories at once.
mkdir name1 name2 name3 name4 name5
Copying Files
The cp command is what we use to copy files or directories.
cp file1 file2
This copies the single file to another file.
cp -a file1 file2
The option -a copies a file with all of its attributes to another file.
cp -i file1 file2
The option -i will prompt the user for confirmation when overwriting a file.
cp -r folder1 folder2
The -r option will copy folders and all of their contents.
cp -u file1 file2
The -u option will only copy files that do not exist or are newer than the
existing correspondinf files in the destination directory.
cp -v folder1 folder2
The -v option will display extra information as copying is done.
Moving Files
We move and rename files with the mv command. So, the mv command can be used in
multiple ways.
mv file1 file2
This will rename file1 to file2.
mv file1 folder1
When used like this, it moves file1 to folder 1.
mv -i file1 file1
The -i option will confirm you want to complete the action.
mv -u file1 file2
This will again only move files that do not exist or are newer than the files in
the destination folder.
mv -v file1 folder1
The -v will also give extra information when moving file1 to folder 1.
Removing Files
The rm command is used to remove files and folders.
rm file1
That will remove a file.
rm -i file1
This will ask for confirmation because of the -i option.
rm -r folder1
This will remove a folder and all of its subdirectories. You must use this
option to delete folders.
rm -v file1
The -v gives extra information when performing this task.
Creating Links
We create links using the ln command. Links can be either hard or soft. Hard
links are an older way of doing things, while soft links are the modern way.
This creates a hard link:
ln file link
This creates a soft link:
ln -s file link
ln -s folder link
As you can see, you cannot make a hard link of a folder or directory. That must
be done with a soft link.
Every file has a hard link associated with it. When a hard link is created, we
are making another way to refernce the file. Hard links cannot reference
anything outside its original file system. It also cannot reference a directory.
Soft links were made to overcome the limitations of hard links. When you create
a soft link, you are creating a unique file that contains a pointer to the
original file or directory. When you wrtie information to the soft link, the
original file is updated. So unless you go looking, it is hard to tell the
difference between the two. However, when you delete the link, the original file
is untouched. If the file is deleted first, the link stays but points to
nothing.