Materials for Topic 12: File and Directory Management
Full C Programs
get_file_size.c - a C program outputing the size of a file whose name is passed as the 1st argument (argv[1]) to the program.
find_file_type.c - a C program outputing the type of a file whose name is passed as the 1st argument (argv[1]) to the program.
permissions_changing.c - a C program changing the permissions of a file and printing the update to the terminal.
extended_attributes.c - a C program printing all the extended attributes of the file at argv[1] and their values.
switch_directories.c - a C program switching the current directory D to argv[1] and then back to the previous directory D.
my_ls.c - a C program listing the contents of a directory. If no additional arguments are passed, the contents of the current working directory (.) are printed. Otherwise, the contents of the directory whose path is stored in argv[1] are printed.
adding_links.c - a C program that creates a hard link and a soft link for a file given at argv[1]. Both are created in the same directory where the original file is stored. The program then calls ls -il to show that the 2 links were created. Finally, the program removes both links and calls ls -il again.
eject_cd_rom.c - a C program using the CDROMEJECT request to eject the media tray from a CDROM device, which the user provides as the first argument on the program’s command line.
The command: gcc -Wall -Wextra -O2 -g -o program program.c
compiles the C source code located inside the file program.c. See more details here.
The command: . ./.short_prompt
executes code inside a file named .short_prompt and sources it (applies all the changes to the current session.) See more details here.
The command: . ./.long_prompt
executes code inside a file named .long_prompt and sources it (applies all the changes to the current session.) See more details here.
The command: getfattr -d -m ".*" myfile
shows the extended attributes of the file myfile.
The command: ls -i
shows the i-node number of every file and folder inside the current folder.
The command: ls -i *
shows the i-node number of every file inside the current folder, including those files within nested folders.
The command: cat /etc/passwd
prints the content of the /etc/passwd to the screen. Each line in this file contains information about users in the Linux system, such as the username, the preferred shell interpreter, and the home directory.
The command: getent passwd "$( id -u )"
in case the file /etc/passwd doesn't include information about your user, use the command getent passwd "$( id -u )" to search the system for information about your user. It will print a line or a few lines including info only about your user. For example, your instructor sees the following info when calling this command: briskman:x:4846:4846:Miriam Briskman (Miriam Briskman):/u/briskman:/bin/bash
The data are separated by colons (:), and there are 7 of them: (1) the username (briskman), (2), a placeholder for the password "x", (3) the user ID (4846), (4) the group ID (4846), (5) the name of the user (Miriam Briskman (Miriam Briskman)), (6) the home directory (/u/briskman), and (7) the default shell (/bin/bash).
The command: umask
prints to the terminal the permissions that the OS excludes by default from newly-created files. For example, if the output of this command is: 0002 it means that other users won't be allowed to write to or execute newly created files (but they can still read (view) the content of the file.)
The command: rmdir -r folder
deletes all the files and folders inside the folder directory and then deletes folder itself. Without the -r option, the folder directory won't be allowed to be deleted until you empty it from files and folders.
The command: program | less
displays the output from the command given by program in Linux's less content viewer. This is a great option especially when the output from program is very long and can't fit on the screen all at once. To display the contents of a file (e.g., myFile.txt) instead of a program's output, type: less myFile.txt To scroll through the content that less displays, press the up or down arrow keys. To exit the less content viewer, press the 'Q' key on the keyboard.