Everything is a file, know how to cope with them.
File management is about organizing, copying, moving, deleting and renaming files.
To understand file management, one must first understand the filesystem layout and how users and groups are used to set permissions on files and directories.
The Filesystem
A file system (FS) is a complex data structure that dictates the way files are stored on a physical medium. It exists in several flavours, some of them you may already know: FAT, NTFS, ext4, HFS, ISO 9660, … There is no ideal FS, all have pros and cons. The FS you usually use, whether you know it or not, depends on the operating system (OS) you’re on. Nowadays, Windows OSes rely on the NTFS file system, while Linux OSes mostly use ext4 and MacOS has its own APFS to store files.
From a user perspective, the FS is where files and directories are stored (by the way, a directory is nothing but a special file).
This leads to a layout specific to each OS.
The Linux OS inherits the Unix FS layout where all is organized under a unique root directory called / (slash).
This is not the place to list and explain the Linux FS organisation, but we must mention the most commonly found directories and their purposes.
/bin,/sbin,/usr/bin,/usr/sbin-
directories where system commands reside. You can find out in which directory a command is located by using the
whichcommand. /root-
homedir of the superuser.
/etc-
most of the system wide configuration files are stored there.
/home-
directory containing the home directories of regular users.
/lib,/usr/lib-
system wide shared libraries.
Browsing the filesystem
When you have no graphical interface to show you where you are and no mouse to click the files or directory you want to select, you have to do things the classic way.
| Commands used |
|
-
Start your terminal application (whether it is WSL on Windows, Terminal on MacOS or some terminal under Linux).
This note is for MacOS users only.
Chances are the default shell on your operating system is Zsh. You need to switch to Bash as those two shells don’t behave exactly the same way. To do so, issue the following command in your terminal:
$> chsh -s /bin/bashIf this command does not work, you can change the default shell on your operating system following the instructions in Change the default shell in Terminal on Mac.
-
Find out where you are located in the filesystem right after you log in.
-
Make your computer display
Helloon the terminal. -
Change directory to
/tmp(this is a system wide directory to store temporary files) and list its content. -
You can list content in several ways. Reading the manual of the
lscommand, figure out how to display the list of files-
in long format;
-
sorted by time, reversed;
-
both at the same time.
-
-
Travel to the
/directory and get the list of all files and directories located at the root of the filesystem. -
Go back to your home directory (the one you where in when you first logged in). There are several ways to do this. Try to find at least two.
-
From there, display the content of the file named
/etc/services
You’ve just learned the basic commands to move around the FS (cd) and get the content of a directory (ls) or a file (cat).
Another major command you’ve used is man.
All commands are heavily documented through manual pages.
Local file management
Most commands you will be using are related to file management. Very often, you will have to list directory contents, change the current directory, copy files and directories, move or rename files, remove files or directories. Organizing content often requires to create directories and to get information on files.
You have certainly figured out already that most files are text files you may want to read, display on screen or edit (more on that later as there is an entire section dedicated to text editors). These files, amongst others, are really easy to compress and if you want to share a bunch of them with colleagues, you may want to create an archive to send them all at once.
These operations are commonly done using a graphical file manager but can be achieved using the CLI as well.
Some actions are easier to achieve using the CLI, like finding a file by its attributes or searching a file for specific content. You may also want to compare files line by line to find out if they differ.
| Commands used |
|
-
Inside your homedir, create a directory called
LFMand make it your current directory. You can check you reached the right destination using which command? -
Without changing directory, find all the files in
/usr/share/docwith a name starting withREADMEand a size between 2 and 10 kilobytes. -
Copy one of them to your current directory (the one from
gzipwould be great if it appears in the list, but anyone will do). If you are on a Mac and the/usr/share/docdirectory does not exist, use anyREADMEfile on your system whose size is less than 10kB. -
Find out how many characters, words and sentences this file contains (you may want to define what a sentence is).
-
Create a file named
rubish.docxby echoing some text in it (using a redirection we will explain later):$> cat > rubish.docx
Type anything you want (it may make some sense to type something useful) and end your input by hitting kbd:[Ctrl+D] (this is keyboard shortcut to inputting
EOF— End Of File).
-
-
What size is your new file? You can find out using two different easy ways (just use simple well known commands). Give them both, explaining your choice.
-
What kind of app can open this file? What is its type?
-
Print the content of this file in the terminal. Does it contain exactly what you typed in earlier?
-
Create a directory named
tempinside theLFMfolder and move therubish.docxfile there, changing its name torubish.txt.
Remote file management
Working remotely in a connected world
Everything you can do locally about file management can be done remotely (on a distant computer) through a remote login.
Due to increasingly security concerns, the only relevant way to login remotely nowadays is using SSH (the secure shell). It creates an encrypted tunnel from your local computer to the remote host. Of course, to be able to log remotely, you must have a user account on the distant computer.
Using the SSH protocol, it is also easy to copy files from and to a remote computer or to synchronize directories. But, as most of the servers are HTTP based, you can as well use command line browsers to get and put files there.
| Commands |
|
-
Using
sshlog into the remote computer namedens-srv-eftcl.emse.fr(use credentials from the school).
Unfortunately, this machine is only accessible from the school (wired network, eduroam WiFi network and emse-invite WiFi network).
|
-
On the remote computer, create a
RFMdirectory under your homedir and make it you current directory. Then, using command line tools only, download the filehttps://ci.mines-stetienne.fr/cps2/course/cls/assets/data/lorem.ipsumand name itlorem_ipsum.txt. -
Make a copy of this file named
place holder.txt(mind the white space in the file name and deal with it), then find out how many words and sentences it contains (this time you may consider a sentence as a part of the text ending with the.character). -
From another terminal, copy the local
LFM/READMEto your remoteRFMdirectory and compare it with the same file on the distant system if it exists. If the file on one side is compressed, you must decompress it before making the actual comparison. -
Synchronize the
tempdirectory and its content with the remote computer (put it in theRFMfolder). -
Make some changes to the content of the
tmpfolder (add files, make changes to files already there, …) of this hierarchy and try to synchronize again. -
Then, make some changes remotely and synchronize the other way (from remote to local).
|
If you are not comfortable yet with command line text editors, you can simply add content to a text file by echoing something at the end of it, using redirections. |