
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Listing modified, old and newly created files on Linux using C++
Here we will see how to list the modified files and old and newly created files on Linux platform using C++ program.
The task is very simple. We can use the Linux shell command to get the files in desired order. The ls –l command is used to get all of the files in long listing format. Here we will add more options to sort them based on time. (Ascending and Descending). The –t command is used to sort based on time, and –r can be added to reverse the sequence.
The command will be like below:
ls –lt ls –ltr
We will use these commands using the system() function in C++, to get the result from C++ code.
Example Code
#include<iostream> using namespace std; main(){ //Show the files stored in current directory descending order of their modification time cout << "Files List (First one is newest)" << endl; system("ls -lt"); //use linux command to show the file list, sorted on time cout << "\n\nFiles List (First one is oldest)" << endl; system("ls -ltr"); //use the previous command -r is used for reverse order }
Output
Files List (First one is newest) total 32 -rwxr-xr-x 1 soumyadeep soumyadeep 8984 May 11 15:19 a.out -rw-r--r-- 1 soumyadeep soumyadeep 424 May 11 15:19 linux_mod_list.cpp -rw-r--r-- 1 soumyadeep soumyadeep 1481 May 4 17:03 test.cpp -rw-r--r-- 1 soumyadeep soumyadeep 710 May 4 16:51 caught_interrupt.cpp -rw-r--r-- 1 soumyadeep soumyadeep 557 May 4 16:34 trim.cpp -rw-r--r-- 1 soumyadeep soumyadeep 1204 May 4 16:24 1325.test.cpp Files List (First one is oldest) total 32 -rw-r--r-- 1 soumyadeep soumyadeep 1204 May 4 16:24 1325.test.cpp -rw-r--r-- 1 soumyadeep soumyadeep 557 May 4 16:34 trim.cpp -rw-r--r-- 1 soumyadeep soumyadeep 710 May 4 16:51 caught_interrupt.cpp -rw-r--r-- 1 soumyadeep soumyadeep 1481 May 4 17:03 test.cpp -rw-r--r-- 1 soumyadeep soumyadeep 424 May 11 15:19 linux_mod_list.cpp -rwxr-xr-x 1 soumyadeep soumyadeep 8984 May 11 15:19 a.out
- Related Articles
- Listing out directories and files using C#
- Learn how to find and list down recently modified files in linux
- Find and tar Files on Linux
- Listing out directories and files in Python?
- Filtering Files Copied When Using rsync on Linux
- Create and display the newly created database in MongoDB?
- Find and Convert Files Ending With CRLF on Linux
- The best way to compress and extract files using the tar command on linux
- Move all files except one on Linux
- MongoDB - How to copy rows into a newly created collection?
- Soft-Delete Files from the Terminal on Linux
- Find the Largest Top 10 Files and Directories On a Linux
- How to find and sort files based on modification date and time in linux
- Delete empty files and directories in Linux
- How to search contents of multiple pdf files on Linux?

Advertisements