Skip to content
Advertisement

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.

Advertisement

Answer

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
    
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement