简体   繁体   中英

Getting the User ID from the last line of the /etc/passwd file in Linux

I've been trying the following:

cut -d: -f3 | last -1 /etc/passwd

and

last -1 | cut -d: -f3 /etc/passwd

These statements aren't working, I'm not sure how to join both of them to get the result I want. It just takes the current command that is in front of the /etc/passwd directory.

I'm fairly new to Linux and combining commands together.

Thank you for the help in advance.

Try:

cut -d: -f3 /etc/passwd | tail -1

Or:

tail -1 /etc/passwd | cut -d: -f3

Notes

  1. The command last shows a listing of last logged in users. By contrast, tail provides the end of a file and tail -1 provides just the last line.

  2. Consider this command:

     cut -d: -f3 | last -1 /etc/passwd

    This runs cut -d: -f3 but since in file names are provided, cut will wait for you to provide input on stdin. This is not what you want. By contrast, the command below provides the file /etc/passwd as input to cut and then selects the last line of cut's output:

     cut -d: -f3 /etc/passwd | tail -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM