Skip to main content

Moving around Filesystem

The most basic part of using a command interpreter, such as bash, is to be able to move around the Linux filesystem. It is important to understand how bash lets you represent directories. This is done in two different ways: using absolute paths and relative paths.

Absolute Paths

An absolute path is a path that describes the location of a file or folder starting from / (root). This is easy to spot as it will always start with /.

An example of an absolute path is /home/pi/Desktop. This path is the folder that contains all the items on your desktop.

Relative Paths

A relative path is a path to the file or folder that is described based on where you are in the filesystem.

An example of a relative path is ../Desktop. This means that a folder called Desktop is stored in a folder that is one level above the one you are in.

There are several special paths that you should know of in bash, as follows:

Path Description
/ This is the root of the Linux filesystem and the highest path that you can have.
./ This represents the directory that you are currently in.
../ This is the directory one level below the one you are in.
~ This represents your home directory. When you are logged in as the Pi user, this will be /home/pi. If you are running a command as the root, this will be / root.

Manipulating files

Now that you have an understanding of how paths are represented in bash, it is time to learn how to manipulate them.

There are many commands that you can use to manipulate files in bash, some of them are following:

ls

ls is perhaps the most common basic function, and is used regularly to see which files are present in the directory that you are in. This is done using the list (ls) command. By default, this is the current folder that you are in. If you want to see the contents of another folder, simply append the path to the end of the ls command.
The contents of the home directory

ls doesn't display any files or folders that begin with (.). The reason for this is that any file or directories beginning with (.) are hidden.

The ls command has many different options that change the output on your screen. Some common ls parameters are as follows:

Option Description
-a This shows hidden files and folders
-l This uses the long list format. This will show file sizes, file permissions, and the owners of the files
-h This shows all the files sized in a human-readable format

cd

The Linux filesystem is made up of many different files and folders. When using bash, it is really important to be able to move around the filesystem. This is done using the cd (change directory) command.

cd is easy to use. All you need to do in order to change the current directory is to run cd followed by the path of the folder you want to move into. This can either be an absolute directory or a relative directory.
Changing Directory
The directory that you are in after you run cd is called the current working directory. Unless you specify, a directory bash will look for any commands that you run and any files that you are using in the current working directory. If the command is not in the current folder, bash will look in several other predefined folders for the command. These folders are specified in the PATH environment variable in bash. From the knowledge of Relative Paths:
cd / takes you to root directory
cd ~ takes you to your home (if you are using default user settings home directory is /home/pi directory)  directory. The cd command alone also does the same job.
cd .. takes you one level up from current directory and
cd ../ takes you one step below to current directory.

While using these commands there is a chance for you get confused which is the current directory you working in. The next command will resolve this problem by printing current directory you are working in.

I believes you will try the relative paths with the ls command and observe the results. You have to observe these ls commands with relative paths changes the directory or not also.

pwd

Sometimes, when navigating the Linux filesystem, you might need to find out which directory you are in. You can do this using the pwd (print working directory) command.
Print Working Directory

cat

When you are working with files, sometimes it's very handy to quickly view the contents of a file. The cat command does exactly this. When you run the cat command followed by a filename, it will display the contents of that file on the screen. Here the cat stands for catalogue.
The cat command
cat has several options that you can use to customize the format of the file on the screen. Some of the cat command's parameters are as follows:
Option Description
-n This shows line numbers in the file
-E This adds $ at the end of every line so that you can see where it is

Remember that everything in Linux is a file. Using the cat command, you can read in data from the device on your Raspberry Pi, including its serial port and camera. By running cat /dev/video0 > video.record, you can record the video coming in from your camera.

head and tail

Another handy set of commands that you can use to see the contents of a file are head and tail. The head and tail commands go together and show the start of the end of a file. The head command is extremely useful if you want to see whether a big file is the file that you are looking for and it is too big to be shown using cat. The tail command is extremely handy if you want to look at the last few entries in a log file.
The head and tail command
The head and tail commands have many different options that you can use in order to customize the output format of the file on the screen. The most important is the –n option. The -n option followed by a number will display n lines of the file. You can find the other options by running man head or man tail.

cp

cp is another important command in bash. cp copies a file or folder from one folder to another. By default, cp will overwrite a file in the destination folder if it already exists, so beware!

To use cp, you first need to specify the file or folder that you want to copy followed by the destination folder. If you select a directory, it will copy just this directory. If you want to make an exact copy of the directory, you need to use the –r command. The –r command recursively copies the folder.

Basically, what this command does is it makes an exact copy of the folder you are copying, so you will most likely want to use this option whenever you are copying a directory.
Copy a file and folder
The cp command has many different options that change how it copies a file or directory, as shown in the following table:
Option Description
-r This recursively copies a file or directory
-f If an existing file in the destination directory can't be opened, this removes it and copies it again
-p This preserves the owner and timestamp of the files that you are copying
-v This indicates verbose mode, it shows the names of the files that are being copied

mv

Sometimes, you don't want to copy a file or directory, but instead you want to move it around. The mv command does exactly that. Its syntax is exactly the same as the syntax of cp. To move a file or folder, it is simply a matter of doing as shown following:
mb command
Unlike cp, mv automatically moves the whole file or folder and it doesn't have an –r option. There are several other options available in mv, shown as follows:
Option Description
-f This overwrites any files and folders that are in the destination location
-u This only moves the file if it is newer that the file in the destination folder

rm

Sometimes, a file or directory is no longer required and needs to be deleted. To do this, the rm command is used.
rm command
To delete a file or folder, simply run the rm command followed by the file or directory that you want to delete. Raspbian and most other Linux distributions don't contain any sort of recycle bin functionality. This means that when you delete something, it is permanently deleted.

😭There are very few things that you can do to destroy a Linux distribution. The sudo rm –rf / command is one of them. This command will start at the root directory and delete every file on your device, including any files on any removable and network drives attached to your Raspberry Pi. Needless to say, be very careful when using the –r and–f flags.

By default, when removing a directory, rm will stop if there are any files in the directory. To delete the directory, you will need to use the –f flag in order to force the deletion of the folder. Some of the rm command's parameters are as follows:
Option Description
-f This never prompts when deleting any files
-i This prompts before deleting each and every file
-r This recursively deletes the files and folders

mkdir

You will often need to create a new folder in order to keep your files organized. The purpose of using mkdir (make directory) is exactly that. To use mkdir, all you need to do is run mkdir followed by the name of the directory that you want to create.
Using mkdir command
Once you have created the directory, it is immediately ready for use. You can use it to keep your files organized. Some of the mkdir parameters of are as follows:
Option Description
-p This creates any parent directories, if necessary—for example, mkdir new/ directory/with/parents –p
-v This displays a message for every directory that is created

rmdir

This command will remove empty directories. The syntax for this command is rmdir name_of _directory.  The command rmdir NewDir only removes the NewDir iff it is an empty directory.

touch

touch is a simple command that creates an empty file if a file doesn't exist. If the file exists, it updates the access time to when the command is run.

touch is often used when a file needs to exist. Some examples of this include creating empty log files. In order to use touch, run touch followed by the filename that you want to create or update the access time of.
Touch command

Discussion

How can I rename a file?
You can rename the file using mv command the syntax is as follows:
mv path1/file.txt path2/file2.txt
The path one can be path of any directory for example if path1 is home directory you should /home/pi instead path1. If path2 is your desktop you should type /home/pi/Desktop or ~/desktop instead path2. The file2.txt is new name of file. If you executed this command you can see that the file2.txt will poping out on your Desktop.
•You must try this syntax with cp command and then observe the results.
Naming of Linux files and directories
Following are general rules for both Linux, and Unix like systems:
  1. All file names are case sensitive. So filename arun.txt Arun.txt ARUN.txt all are three different files like Widows system.
  2. You can use upper and lowercase letters, numbers, “.” (dot), and “_” (underscore) symbols.
  3. You can use other special characters such as blank space, but they are hard to use and it is better to avoid them. I am not preferring you to put blank space on file names like arun k s.txt or create directory using mkdir arun k s. It is better to use some characters as discussed above.
  4. In short, filenames may contain any character except / (root directory), which is reserved as the separator between files and directories in a pathname. You cannot use the null character.
  5. No need to use . (dot) in a filename. Some time dot improves readability of filenames. And you can use dot based filename extension to identify file. For example:
    • .sh = Shell file
    • .tar.gz = Compressed archive
  6. Most modern Linux and UNIX limit filename to 255 characters (255 bytes). However, some older version of UNIX system limits filenames to 14 characters only.
  7. A filename must be unique inside its directory. For example, inside /home/pi directory you cannot create a demo.txt file and demo.txt directory name. However, other directory may have files with the same names. For example, you can create demo.txt directory in /tmp.

Comments

Popular posts from this blog

8051 Simple Programs

Sum of 8-bit Numbers Stored in Memory Here  we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click  here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel.  Program ORG 00H MOV R0,#50H   ; get memory location in memory pointer R0 MOV R1,#51H   ; get memory location on memory pointer register R1 MOV A,@R0       ; get content of memory location 50H to accumulator ADD A,@R1        ; add content of A with content of memory location 51H and store result in A MOV R0,#52H    ; get 52H to memory pointer R0 MOV@R0,A         ; copy content of A to memory location 52H END Add 16-bit Numbers ...

Introduction to 8051 embedded C

For 8051 we need to include the file reg51.h. This file contains the all the definitions of 8051 registers. With this information C compiler produces hex file that can be downloaded into the ROM of the microcontroller. It is important to note that the size of the hex file produced by the assembly language is much larger than the hex file produced by C compiler. Apart from this fact, there is many reasons for writing programs in C instead of assembly: ●It is much easier and less time consuming to write programs in C assembly. ●C is more flexible; it is easier to modify and update. ●Programming in C allows to use code available in function libraries. ●Program written inC for one microcontroller is portable to other microcontrollers with little or no modifications. Data Types in 8051 Embedded C The table shown below lists the data types that are available in typical C51 compiler. The gives information about the size of the data variable and it's value range. Data type ...

Frequency of Oscillation of RC Phase Shift Oscillator

Derivation of Frequency of Oscillation We have to find out the transfer function of RC feedback network. Feedback Circuit of RC Phase Shift Oscillator Applying KVL to various loops on the figure, we get, $$I_1 \left(R+\frac{1}{j \omega C }\right) -I_2R=V_i \text{ ....(1)}$$ $$-I_1R+I_2\left (2R+\frac {1}{j\omega C}\right)-I_3R=0\text{ ... (2)}$$ $$0-I_2R+I_3\left(2R+ \frac{1}{j\omega C}\right)=0\text{ ...(3)}$$ Replacing \(j\omega\) with \(s\) and writing equations in the matrix form, $$\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}\begin{bmatrix}I_1\\I_2\\I_3\end{bmatrix}=\begin{bmatrix}V_i\\0\\0\end{bmatrix}$$ Using Cramer's rule to find out \(I_3\), $$\text{Let, }D=\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}$$ \(|D|=\begin{vmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{...