Basic UNIX/Linux file management

From Knowledge Kitchen
Jump to navigation Jump to search


This document outlines some basic commands that allow a user of a UNIX-like operating system to manage their files.

Windows users may be interested in an equivalent overview of managing files from the command line on Windows operating systems.

Assumptions

To be able to issue UNIX commands, a user must either either run a UNIX or Linux command shell locally on their own computer or be already remotely logged in to a UNIX or Linux machine. There are thus a variety of ways to access a UNIX or Linux command shell:

Locally:

  • using the Terminal app on Mac OS X (Located within Applications / Utilities)
  • using Git Bash, which is installed as part of Git for Windows
  • using CygWin, which is a similar Linux emulator for Windows
  • using the Windows Subsystem for Linux available to install on Windows 10+
  • using any of a variety of shells available on all UNIX or Linux machines

Remotely:

Print the current working directory

Show what directory is the current working directory:

pwd

Change current working directory

Go to your personal home directory (every user has their own):

cd

Go one level up:

cd ..

Go one level down into the 'foo' subdirectory:

cd foo

Go to the very top directory of the drive (called the root directory)

cd /

Go to the very top of the hard drive, then through the 'Users' subdirectory and into the 'foo' subdirectory

cd /Users/foo

Go to the current directory (in other words, go nowhere)

cd ./


List files and directories

Show the names of files and folders in the current working directory:

ls

Show the names of all files and folders, including hidden files and folders, in the current directory:

ls -a

Show all the details of the files and folders in the current directory:

ls -l

Show all the details of all files and folders, including hidden files and folders, in the current directory:

ls -la

Show all details of all files and folders in the 'Users' subdirectory of the root (top-most) directory:

ls -la /Users

Clear the screen

clear


Copy a file or directory

Copy all the contents of the bob1 directory to the bob2 directory:

cp bob1 bob2

Copy a file from one location to another:

cp /home/scps/bob/file1.txt ./file1.txt


Move a file or directory from one location to another

Move (i.e. rename) a folder named bob1 to a folder named bob2:

mv bob1 bob2

Move a file from one location to another:

mv /home/scps/foo/file1.txt /home/scps/bar/file2.txt


Delete a file or directory

Delete a file named bob1:

rm bob1

Delete a directory named bob2:

rmdir bob2

Create a directory

Examples:

mkdir foo
mkdir ../foo/bar


Create a file

Empty file

Create an empty file - a file with no data in it:

touch a_nice_and_empty_file.txt

Text file

Various common *NIX text editors can be used to create or edit text files.

Run the emacs text editor program (see an Emacs shortcut cheat sheet for help using this editor):

emacs

Run the vi text editor program:

vi

Remotely log into another computer

Remotely log into a server named someserver.somewhere.edu with the username bob:

ssh -l bob someserver.somewhere.edu

This is the same command many NYU CS students use to remotely log into the i6.cims.nyu.edu server.


Change permissions of any file or directory

The chmod command allows UNIX users to change the permissions of any given file or directory.

Make the file named index.html readable by "others":

chmod o+r index.html

Make the file named index.html non-readable by "others":

chmod o-r index.html

Make the file named bob1.txt executable by the user who owns it:

chmod u+x bob1.txt

Make the file named bob2.txt readable, writeable, and executable by the user who owns it:

chmod u+rwx ../foo/bob2.txt


Change ownership of any file or directory

chown

The chown command allows a user to change which user owns any given file.

Set the user named 'slava' to be the owner of the file named 'clown.txt'

chown slava clown.txt

Set the user named 'slava' to be the owner of the entire directory named 'clown_stuff', including all sub-directories and files

chown -R slava clown_stuff


chgrp

The chgrp command allows a user to change which group is associated with any given file.

Set the group named 'clowns' to be the owner of the file named 'slava.txt'

chgrp clowns slava.txt

Set the group named 'clowns' to be the owner of the entire directory named 'slava_stuff', including all sub-directories and files

chgrp -R clowns slava_stuff


Read the documentation of any UNIX command

Examples:

Look up the documentation for the ls command:

man ls

Look up the documentation for the chmod command:

man chmod

Look up the documentation for the cd command:

man cd


Run a Java program

Assuming there is a file named HelloWorld.java in the current directory...

Compile a Java program

Compile the source code into a bytecode (*.class) file:

javac HelloWorld.java

Run it

Running a Java bytecode file means sending it to the Java Virtual Machine to be interpreted into machine code:

java HelloWorld

A note about using packages

If your Java source code indicates a package name, then the source and class files must be located within a folder appropriate to the package name. Every component of the package name must have its own folder.

For example, if the source file is HelloWorld.java and the package is named foo.bar, then you must have the following two files in the following folders:

foo/bar/HelloWorld.java
foo/bar/HelloWorld.class

To execute the program, you must run it as follows. It is critical to execute this statement from outside the top-level package folder... in this case from the parent folder of the folder named 'foo':

java -classpath . foo.bar.HelloWorld

This makes sure that the Java Virtual Machine can find the class file in its proper package.


What links here