Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

questions I need help with for my Linux tools class. 1.) How would you display a

ID: 3699068 • Letter: Q

Question

questions I need help with for my Linux tools class.

1.) How would you display a list of users currently logged into the system, such that every username (and just the username) appears only once? Give exact syntax.

2.) How would you display all files in current directory that are newer than "file.txt"? Give exact syntax

3.) What is the 'time' command used for in Linux?

4.) How would you compress a file (using any of the Linux compression tools available) named "mydata.txt"? Give exact syntax.

5.) Using grep and regular expressions, how would you display the lines of a file named "mydata.txt" that end in A? Give exact syntax.

Explanation / Answer

1)Answer:

3 ways to list all currently logged in users in linux:

w command

The w command is a combination of some of the commonly used Linux commands, such as who, ps and uptime and provides a comprehensive output of the logged in users.

bash$ w

The w command, without any command line options will show you a list of all the users who are currently logged in to system and the information about all the processes that are currently running. By default it should show you the user name, the login time, the CPU resources consumed by each process, the terminal (or tty) and the executing command.

bash$ w <username>

Specifying a particular username to the w command will let you see all the sessions of that particular user and just the processes run by that user. Substitute the username field in the example above with the actual login name of the user.

who command

Another command that you can use is the who command, which is closely related to the wcommand above. Compared to the w command, the who will print out much more concise information. It usually skips the detailed process information by default and shows you the user name and the tty information.

There are some useful command line options available with the who command…

-b (or –boot) : this will print out the time of the last system boot
-d (or –dead): prints out some information about processes that are dead or defunct
-a (or –all): this enables a bunch of useful options such as -b, -d, -l, -p etc.

bash$ who -a

This will list all logged in users along with detailed information about each session and terminal.

users command

Sometimes, you just want to know the logins of the users who are currently logged in. The users command will print out just the user or login names with out any of the other information such as login times and process information.

bash$ users

This will list all logged in users. However, if a user has multiple sessions then it will print out the user name for each session. The previously mentioned commands w and who also outputs the user name for each session. It shows looks redundant or repetitive in this case as there is no additional information about the session.

Output Customization

Sometimes, the purpose of listing the currently logged in users is to feed it as input to another script or another process. You might be interested in just the username listed just once and uniquely.

You can use several other Linux utilities to do this, such as cut, sort, awk, tr and uniq. We will use some of them below to demonstrate how it works.

bash$ who | cut -d' ' -f1 | sort | uniq

You can use the above output formatting with the w command as well. This will list all logged in users sorted by username and each will be listed just once. Below is the similar command but this time using the w command.

bash$ w -h | cut -d' ' -f1 | sort | uniq

Some of the other very generic examples using other commands such as users and who are…

bash$ users | tr ' ' ' ' | sort -u

bash$ who | awk '{ print $1 }' | sort | uniq

Depending on your requirement, you can further play around with the options of the cut utility to show more fields, such as

bash$ who | cut -d' ' -f1,5 | sort | uniq

Another command that is probably worth mentioning here is last. The last command will display a (usually long) list of all users that were logged-in in the past. This list will contain both the users that are currently logged-in and active as well as users who are not currently logged-in. Use the command line argument -n to limit the list.

bash$ last -n 100

The above command will show the last 100 users whohave logged into the system.

3)Answer:

The time command runs the specified program command with the given arguments. When command finishes, time writes a message to standard output giving timing statistics about this program run. These statistics consist of:

time syntax

Options

time examples

-p If -p is specified, the timing summary is printed in a portable POSIX format.