Week 5: Files, Directories, and Pointers Flashcards
What is a file?
A container for data which is persistent and accessible by name
What are UNIX files?
Unix file types: regular, directory, device, link and
etc.
Unix files are identified by a name in a directory
What is a “Regular” file?
essentially a sequence of bytes
can access these bytes in order
What is a “Directory” file?
a file containing name and pointer (inode number)
pairs for files and subdirectories in the directory
What is a “Device” file?
access a device (like a soundcard, mouse,
terminal, or …) like it is a file
What are “Links”
hard link: create another name for a file
soft link: a pointer to another file
What option can be used to get file information?
-F
What does the UNIX file system represent? What represents the root?
An upside-down tree
/ represents root
What is the difference between an absolute pathname and a relative pathname?
Absolute: to identify where a file is, concatenate the directories
together, can be anywhere in the UNIX file system
Relative: pathnames relative to the current working
directory
What pathways signify home?
$HOME
/user/home
~
or simply cd
What do the following represent in file locations?
/
~
.
..
/ means root
~ means home
. means current directory
.. means parent directory
What does mkdir dirname do?
Makes a new directory in the current directory with the name “dirname”
What does rmdir do? When does it work? Not work?
Removes a directory, only works if the directory exists and is empty
What does cp text1 text2 do if:
text1 is a file
text2 is a directory?
Copies text1 to directory text2
What does cp text1 text2 do if:
text1 is a directory
text2 is a directory?
Nothing, you need to add the
-r option to make it recursive:
cp -r text1 text2
What does cp -r text1 text2 do if:
text1 is a directory
text2 is a directory?
Copies directory text1 and all of its contents to directory text2
What is the command to create a link? Format?
ln readme.txt unix_is_easy
Creates a link to readme.txt called unix_is_easy
What is a hard link?
An indirect pointer to another file or directory
What command searches through directories for a file? Format?
find
find PATH EXPRESSION
e.g. Find all files and directories named README
starting from the current directory:
find . –name “README”
Note: the dot before -name
What does ls -a do?
list all files including those
beginning with a . (dot)
What does head do?
head displays the
top lines of a file
What command can you use within a “man” veiwer to go to the next page?
Spacebar
What command can you use within a “man” veiwer to quit?
q
What characters are valid as file names?
Almost any character is valid in a file name, the exception is the / (slash) character and null character
Are UNIX file names case-sensitive?
Yes
gcc looks for what extensions by default?
.c for C program files
.h for header files
.o for an object file
What does the file command do?
file filename
Print the type of the file
How do you delete a file with the name -i?
rm – -i
– means no options
For wildcarding, what does * mean (asterisk)
matches a sequence of zero or more characters
For wildcarding, what does ? mean
Matches any single character
For wildcarding, what does […] mean
Matches any of the one characters between the braces
What do double quotes do in shell?
Stops interpretation of some
shell special characters (whitespace mostly, *, ~)
What do single quotes do in shell?
Stops interpretation of even more specials like $
What does Ctrl-V do in shell?
Quotes the next character, even if it is a control
character
What are the three levels of file permission?
user = the owner of the file
group users = a group of people set by the user
others = everyone else
What command shows file permission?
ls -l
What is this?
drw-r-xr–
First character (d here) indicates file type
The rest is permission in the order of user, group, others
In the order of rwx (read write execute)
What is the syntax (using numbers) of chmod and what does it do?
Sets file permissions
chmod DDD file [file …]
chmod 644 file
Where the three numbers (up to 7) indicate binary numbers
What is the symbolic syntax of chmod?
chmod [ugoa][+-=][rwx] file […]
[ugoa] indicates who
[+-=] indicates add, remove, or exactly equal to
[rwx] indicates what permission(s)
What does the -R flag do for chmod?
It recursively changes the mode for each chmod operand that is a directory.
What is umask? How does it work?
Sets the default permissions for any file
you will create
Format is backwards to the chmod command
– tells you which permissions will NOT be given
umask 077 means don’t let anyone but the User
do anything with my files by default
Pointers must have a
Type
What does an * do for pointers?
Declare a pointer AND what the address points to
What does an & do for pointers?
Return the address of the variable
All pointer variables in a 16-bit architecture are … bytes
2 bytes
All pointer variables in a 32-bit architecture are … bytes
4 bytes
Pointer variables contain
The address in memory of another variable
All pointer variables in a 64-bit architecture are … bytes
8 bytes
What does
int j = *&i;
do?
/* same as j = i; */
What does the following do?
char ca[3],*cap;
cap = &(ca[1]);
*cap = 7;
Creates a character array with 3 elements and a character pointer called cap
The pointer cap is assigned the address of the SECOND element in the array
Go to the address location and make it 7
In pointer arithmetic, what is important
The type of the variable
How many bytes will the following pointer arithmetic move if the array contains integers?
cp = &(ca[0]); *(cp+2) = 8;
8 bytes (2 x 4 bytes)
How many bytes will the following pointer arithmetic move if the array contains characters?
cp = &(ca[0]); *(cp+2) = 8;
2 bytes (2 x 1 bytes)
How many bytes will the following pointer arithmetic move if the array contains double floats?
cp = &(ca[0]); *(cp+2) = 8;
16 bytes (2 x 8 bytes)
What is a void pointer? How do you reference it?
Declared by: void *pV;
A void pointer in c is called a generic pointer, it has no associated data type.
printf(“c = %c\n\n”,((char)pV));
printf(“db = %lf\n\n”,*((double *)pV));
What is an inode number?
A pointer to a directory in UNIX