0% found this document useful (0 votes)
6 views

Chip Designer's Code _ Linux CLI _ Part 3

Chapter 2 of the document covers file handling in Linux, detailing commands such as mkdir, touch, ln, mv, cp, and rsync for managing files and directories. It explains the creation of directories, file timestamps, symbolic and hard links, and their practical applications. The chapter emphasizes the importance of these commands for efficient file management in VLSI design and programming.

Uploaded by

Shivam Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chip Designer's Code _ Linux CLI _ Part 3

Chapter 2 of the document covers file handling in Linux, detailing commands such as mkdir, touch, ln, mv, cp, and rsync for managing files and directories. It explains the creation of directories, file timestamps, symbolic and hard links, and their practical applications. The chapter emphasizes the importance of these commands for efficient file management in VLSI design and programming.

Uploaded by

Shivam Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Chip Designer’s Code

Automation and Programming in VLSI


Chapter 2 : Linux – Part 3 : File Handling

LINUX
Command Line
TCL PYTHON REGEX
&
Scripting

Amr Adel Mohammady


VCS /amradelm

/amradelm
MAKEFILE
Save The Palestinian Children
The Colonial State of Israel Killed +100 children in less than a day
“Hundreds of people have reportedly been killed,
including more than 130 children, representing
one of the largest single-day child death toll in the
last year.

Some of the strikes reportedly hit makeshift


shelters with sleeping children and families.”

-UNICEF Executive Director


– 18 March 2025
Source : UNICEF

“Trump fully supports Israel and the IDF and the actions that they've taken in recent days”

-White House
Source : France24 2
Introduction

• In the previous part we covered the Linux File System and the important files and directories in Linux
• In this part, we will discuss the commands used to handle files and directories such as creating, moving, or deleting them.
• Commands Covered:
o mkdir
o touch
o ln
o mv
o cp
o rsync
o rm

/amradelm

/amradelm 3
mkdir

/amradelm

/amradelm 4
mkdir

• Creates new directories (folders) in the filesystem.


• Syntax : mkdir [options] directory_name
o Creates a new directory called <directory_name>. If the directory already exists, you get an error
o You can create multiple directories at once : mkdir dir1 dir2 dir3
• Common Options:
o -p : Creates nested (hierarchical) directories as needed; doesn’t error if they already exist.
▪ Example : mkdir -p /home/amr_adel/projects/rtl
o -v : Enable verbose mode which prints a message for each directory created.

-p (parent) and –v (verbose)

/amradelm

/amradelm 5
touch

/amradelm

/amradelm 6
touch
• Creates empty files or updates the timestamp1 (access/modification time) of existing ones.
• Syntax : touch [options] file_name
Modification time
o Creates a new file called <file_name>. If the file already exists, updated

the timestamp is updated


o You can touch multiple files at once touch file1 file2 file3

• Common Options:
o -a : Updates only the access time.
▪ Example: touch -a existing_file.txt
o -m : Updates only the modification time.
Updating To A Specific Modification Time Only
▪ Example: touch -m existing_data.log : By Combining –mt Options
(stat command prints all timestamps of a file)
o -t : Update to a specific timestamp (format: [[CC]YY]MMDDhhmm[.ss]).
▪ Example: touch -t 199903151200 file.txt (sets to March 15, 1999, 12:00).

• Why Is It Useful?
o Pre-create files that your application will later populate to avoid "file not found" errors during setup
o Triggering Script Actions : Some scripts and programs like Makefiles check a file’s modification time to decide whether to run or not. Updating the timestamp
will force them to rerun.

/amradelm

[1] Timestamps will be discussed in a later part /amradelm 7


ln
(Symlinks)

/amradelm

/amradelm 8
ln
• Creates links between files or directories. A symbolic link (symlink) is like a shortcut pointing to another file or directory
• Syntax : ln [options] target link_name
o Creates a new symlink (shortcut) called <link_name>. If the link already exists, raise an error

o Different forms:
1. ln /home/my_file.cpp /dir/link_name

Create a link under /dir/ called link_name pointing to /home/my_file.cpp


2. ln /home/my_file.cpp /dir/

Here the 2nd argument is a directory without a link name. This creates a link under /dir/ called my_file.cpp (same as target) pointing to /home/my_file.cpp
3. ln /home/my_file.cpp my_link

Here the 2nd argument is provided without a directory path. This creates a link under the current working directory called my_link pointing to /home/my_file.cpp
4. ln /home/my_dir /dir/my_dir_link_name

Here the target is a directory. Create a link under /dir/ called my_dir_link_name pointing to directory /home/my_dir/
5. ln /home/my_dir /dir/

Here the 2nd argument is a directory without a link name. This creates a link under /dir/ called my_dir (same as target)” pointing to /home/my_dir/

• Common Options:
o -s : Creates a symbolic (soft) link. Otherwise, creates a hard link (see next slide)
o -f (force): Overwrites an existing link if link_name already exists. Otherwise, raise an error if the link already exists
o -v (verbose): Prints out what is being done

/amradelm

/amradelm 9
ln (Different Forms)

Target
Command Link Location Link Name
Type

ln /home/my_file.cpp /dir/link_name File /dir/ link_name

ln /home/my_file.cpp /dir/ File /dir/ my_file.cpp (same as target)

ln /home/my_file.cpp link_name File Current Working Directory (.) link_name

ln /home/my_dir /dir/my_dir_link_name Directory /dir/ my_dir_link_name

ln /home/my_dir /dir/ Directory /dir/ my_dir (same as target)

ln /home/my_dir my_dir_link_name Directory Current Working Directory (.) my_dir_link_name

/amradelm

/amradelm 10
Hard vs. Soft Links
• To understand the difference, we need to understand what an INODE is:
o An inode is a data structure in a Unix/Linux filesystem that stores metadata about a file or directory.
o The most critical piece of information it holds is the pointer to the file’s actual data on the disk.
o Each inode has a unique number that identifies the file within the filesystem.
o The filename (e.g., my_file.txt) is a human-readable pointer that references the inode.
▪ The filename is called a hard link because it points directly to the inode.
o A soft link (symbolic link or symlink) points to the filename (which in turn points to the inode), making it a pointer to a pointer.
▪ Unlike a hard link, a symlink does not directly reference the inode. The symlink is called a “soft link”

/amradelm

/amradelm 11
Hard vs. Soft Links – Cont’d
• What happens if you delete a file?
o Deleting a file removes its hard link (the filename pointing to the inode).
o The link count (number of hard links pointing to the inode) is decremented.
o If the link count reaches zero, the inode is marked as free, and its data blocks can be reused.
o Until the inode and data blocks are overwritten, the file’s data remains on the disk but is inaccessible through
normal means1.
• What happens if you delete a soft link to a file?
o The soft link is deleted but the hard link it is pointing to remains unchanged.
o The inode also remains safe from deletion

[1] The data remains in the disk but no pointer is referring to it. Some software programs can scan the disk and retrieve the data. That’s
/amradelm

/amradelm 12
why disks that contains confidential or sensitive information need advanced deletion to replace the data in the disk with garbage.
Hard vs. Soft Links – Cont’d
• To understand when to use hard links vs soft links, consider this scenario:
1. .User 1 has a file called “file_name”
2. .User 2 creates a hard link to it
3. .User 3 creates a soft link to it
4. .User 1 deletes the file
5. .User 2 can still access the file while user 3 can’t access it anymore User 1 Deletes The File
and is left with a dangling link
• Therefore, use hard links if you want your links to keep working even
if the original file is deleted (data protection).
• Otherwise, use soft links.

/amradelm

/amradelm 13
Real Use Cases of Links

• Links (soft or hard) are very handy for Linux users, especially VLSI engineers. Let’s discuss several use cases for them:

o Simplifying File Access : In windows, you usually access your apps using the shortcuts (links) on the Desktop instead of opening
C:\Program Files\My_app\app.exe . Similarly in Linux, you can use links for easier access of important files

▪ Example:
ln -s /projects/riscv_134/config/core/settings.conf /projects/riscv_134/core.conf
Now, the developer can edit settings.conf without navigating the full path each time. This is common for configs, logs, or scripts you access often.

o Fixing Path Issues for Legacy Applications : An old application expects a file at /old/path/data.db, but the file has moved to /new/path/data.db.

▪ Command: ln -s /new/path/data.db /old/path/data.db

▪ Why: Creates a symlink at the old path, tricking the application into finding the file without moving it. This is a quick fix for legacy software or during
migrations when paths change.

o Protecting Critical Files from Deletion (Hard links) : A sysadmin wants to ensure a critical log file isn’t lost if accidentally deleted.

▪ Command: ln /var/log/critical.log /backup/critical.log

▪ Why: Creates a hard link at /backup/critical.log pointing to the same inode as /var/log/critical.log. If /var/log/critical.log is
deleted, the data remains

/amradelm

/amradelm 14
Real Use Cases of Links – Cont’d
o Version Managment1 :
▪ Applications and files often have multiple versions, some of which may include updates that
could break the system. To manage this, companies typically rely on an IT or CAD engineer
responsible for:
❑ Installing application versions.
❑ Deciding which version is stable and suitable for use.
▪ To add a layer of abstraction, the IT/CAD engineer sets up a symbolic link (symlink)
named e.g. stable_version. This symlink points to the current stable version of the
application or file.
▪ Instead of directly accessing specific versions, engineers use this symlink.
▪ The IT/CAD engineer updates the stable_version symlink whenever a new stable version is
available , ensuring seamless transitions without requiring engineers to change their workflows.
▪ This approach also ensures that all engineers are using the same version, promoting consistency
and uniformity across the team

[1] This example (Python) could be handled with the PATH variable. But in other cases, versioning is done on files and directories (e.g.
/amradelm

/amradelm 15
RTL files version, analog schematic version, PDK version, etc) which is done using links or VCS (e.g Git)
mv
(move)

/amradelm

/amradelm 16
mv
• Moves files or directories from one location to another or renames them (if the source and destination are in the same directory)
• Syntax : mv [options] source destination

o Move file/directory (source) to new place (destination)


o The source can be multiple files/directories, but in that case, the destination has to be one directory
o mv can be thought of as creating a new hard link and deleting the old one.
o Different forms:

Command Destination Location Action

mv old_name.txt new_name.txt Current Working Dir • Renames file

• Moves file.txt to /my_dir/


mv file.txt /my_dir/ /my_dir/
• Since no name is given the original file name is used “file.txt”

• Moves file.txt to /my_dir/


mv file.txt /my_dir/new_name.txt /my_dir/
• Renames it to new_name.txt
mv old_dir_name new_dir_name Current Working Dir • Renames directory

/my_dir/dir_2/
• If a directory with the name dir_2 already exists, dir_1 is placed under it
mv dir_1 /my_dir/dir_2 Or
• If not, dir_1 is placed under /my_dir/ with the new name dir_2
/my_dir/

/amradelm

/amradelm 17
cp
(copy)

/amradelm

/amradelm 18
cp
• Copies files or directories from one location to another.
• Syntax : cp [options] source destination

o Copies file/directory (source) to new place (destination)


o The source can be multiple files/directories, but in that case, the destination has to be one directory
o Different forms:

Command Destination Location Action

• Copies old_name.txt and name it new_name.txt


cp old_name.txt new_name.txt Current Working Dir
• Original file is unchanged

• Copies file.txt to /my_dir/


cp file.txt /my_dir/ /my_dir/
• Since no name is given the original file name is used “file.txt”

• Copies file.txt to /my_dir/


cp file.txt /my_dir/new_name.txt /my_dir/
• Renames it to new_name.txt
• Copies old_dir_name and name it new_dir_name
cp -r old_dir_name new_dir_name Current Working Dir
• Original directory is unchanged

/my_dir/dir_2
• If a directory with the name dir_2 already exists, dir_1 is placed under it
cp -r dir_1 /my_dir/dir_2 Or
• If not, dir_1 is placed under /my_dir/ with the new name dir_2
/my_dir/

/amradelm

/amradelm 19
cp - Options
• Common Options:
o -r : Copy the directory recursively (including its subdirectories and files)
o -p (preserve): Keeps file attributes like timestamps and permissions as the original, otherwise timestamps are updated to the time of copying.
o -u (update) : Only copies if the source is newer than the destination (useful for backups).
o -v (verbose): Prints out what is being done

-p (preserve)

/amradelm

/amradelm 20
rsync

/amradelm

/amradelm 21
rsync
• rsync is also used to copy files or directories but has several advantages over cp shown in the options below.
• Syntax : rsync [options] source destination

o The source can be multiple files/directories, but in that case, the destination has to be one directory
• Common Options:
o -r : Copy the directory recursively (including its subdirectories and files)
o -z (compress): Compresses data during transfer, reducing bandwidth usage (useful for remote syncs).
o -v (verbose) : Prints out what is being done
o -P (progress) : Prints out the progress (%) when copying a large file
o --delete : Deletes files in the destination that don’t exist in the source, ensuring an exact mirror. Think of it as deleting /destination/ before running rsync

-P (progress)

/amradelm

/amradelm 22
rsync
• Common Options:

o --exclude: Excludes specific files or patterns from the sync.


o Example: rsync -r –exclude="*.log" /source/ /dest/ # Exclude all log files from copying1

o --Include: Includes specific files or patterns from the sync.


o Example: rsync -r –include="*.log" –exclude="*" /source/ /dest/ # Exclude everything except log files from copying

--exclude “*.log”

/amradelm

[1] We will learn what the symbol * means in future parts. For now, it is a pattern that means “anything” /amradelm 23
rsync

• Advantages over cp:

o Remote Sync :

• Refers to synchronizing files or directories between two systems connected over a network (which is the case in many software and VLSI companies).

• Example : rsync [options] 192.168.1.100:/remote/path/ /local/path/

IP address of the
remote system

o Include and Exclude : As shown in the previous slide, rsync allows you to include and exclude specific patterns when copying which is very powerful and
allows for quicker copying.

o Example : rsync -r –exclude “*.gds” /source/ /destination/ # Exclude all GDS files from copying

o Resuming Interrupted Transfers: if the transfer is interrupted—just rerun the command, and rsync picks up where it left off instead of starting from zero

o Others : rsync has far more options compared to cp that you can find here : https://linux.die.net/man/1/rsync

/amradelm

/amradelm 24
rm
(remove)

/amradelm

/amradelm 25
rm
• Deletes files and directories permanently (not moved to recycle bin)
• Syntax : rm [options] file_or_directory
o You can delete multiple targets at once : rm file_1 file_2 dir_1 dir_2

• Common Options:
o -r or –R : Deletes directories and their contents recursively, including all subdirectories and files. This option is a must when deleting non empty directories
o -f (force): Removes files without asking (as long as you have permission).
o -i (interactive) : Ask before deleting each file (adding a safety layer)
o -v (verbose) : Prints out what is being done

/amradelm

/amradelm 26
Conclusion
/amradelm

/amradelm 27
Conclusion

• In this part we learned how to handle files and directories in Linux. We learned :
o How to create empty directories and files.
o How to update the timestamps of a file
o How to create links (soft or hard)
o How to copy, move, or delete.

/amradelm

/amradelm 28
Thank You!

/amradelm

/amradelm 29

You might also like